<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><link rel="stylesheet" type="text/css" href="style.css" /><script type="text/javascript" src="highlight.js"></script></head><body><pre><span class="hs-pragma">{-# LANGUAGE Unsafe #-}</span><span>
</span><span id="line-2"></span><span class="hs-pragma">{-# LANGUAGE NoImplicitPrelude
           , BangPatterns
           , MagicHash
           , UnboxedTuples
  #-}</span><span>
</span><span id="line-7"></span><span class="hs-pragma">{-# OPTIONS_HADDOCK not-home #-}</span><span>
</span><span id="line-8"></span><span class="hs-pragma">{-# LANGUAGE StandaloneDeriving #-}</span><span>
</span><span id="line-9"></span><span>
</span><span id="line-10"></span><span class="hs-comment">-----------------------------------------------------------------------------</span><span>
</span><span id="line-11"></span><span class="hs-comment">-- |</span><span>
</span><span id="line-12"></span><span class="hs-comment">-- Module      :  GHC.ForeignPtr</span><span>
</span><span id="line-13"></span><span class="hs-comment">-- Copyright   :  (c) The University of Glasgow, 1992-2003</span><span>
</span><span id="line-14"></span><span class="hs-comment">-- License     :  see libraries/base/LICENSE</span><span>
</span><span id="line-15"></span><span class="hs-comment">--</span><span>
</span><span id="line-16"></span><span class="hs-comment">-- Maintainer  :  cvs-ghc@haskell.org</span><span>
</span><span id="line-17"></span><span class="hs-comment">-- Stability   :  internal</span><span>
</span><span id="line-18"></span><span class="hs-comment">-- Portability :  non-portable (GHC extensions)</span><span>
</span><span id="line-19"></span><span class="hs-comment">--</span><span>
</span><span id="line-20"></span><span class="hs-comment">-- GHC's implementation of the 'ForeignPtr' data type.</span><span>
</span><span id="line-21"></span><span class="hs-comment">--</span><span>
</span><span id="line-22"></span><span class="hs-comment">-----------------------------------------------------------------------------</span><span>
</span><span id="line-23"></span><span>
</span><span id="line-24"></span><span class="hs-keyword">module</span><span> </span><span class="hs-identifier">GHC.ForeignPtr</span><span>
</span><span id="line-25"></span><span>  </span><span class="hs-special">(</span><span>
</span><span id="line-26"></span><span>        </span><span class="annot"><span class="hs-comment">-- * Types</span></span><span>
</span><span id="line-27"></span><span>        </span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier">ForeignPtr</span></a></span><span class="hs-special">(</span><span class="hs-glyph">..</span><span class="hs-special">)</span><span class="hs-special">,</span><span>
</span><span id="line-28"></span><span>        </span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtrContents"><span class="hs-identifier">ForeignPtrContents</span></a></span><span class="hs-special">(</span><span class="hs-glyph">..</span><span class="hs-special">)</span><span class="hs-special">,</span><span>
</span><span id="line-29"></span><span>        </span><span class="annot"><a href="GHC.ForeignPtr.html#Finalizers"><span class="hs-identifier">Finalizers</span></a></span><span class="hs-special">(</span><span class="hs-glyph">..</span><span class="hs-special">)</span><span class="hs-special">,</span><span>
</span><span id="line-30"></span><span>        </span><span class="annot"><a href="GHC.ForeignPtr.html#FinalizerPtr"><span class="hs-identifier">FinalizerPtr</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-31"></span><span>        </span><span class="annot"><a href="GHC.ForeignPtr.html#FinalizerEnvPtr"><span class="hs-identifier">FinalizerEnvPtr</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-32"></span><span>        </span><span class="annot"><span class="hs-comment">-- * Create</span></span><span>
</span><span id="line-33"></span><span>        </span><span class="annot"><a href="GHC.ForeignPtr.html#newForeignPtr_"><span class="hs-identifier">newForeignPtr_</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-34"></span><span>        </span><span class="annot"><a href="GHC.ForeignPtr.html#mallocForeignPtr"><span class="hs-identifier">mallocForeignPtr</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-35"></span><span>        </span><span class="annot"><a href="GHC.ForeignPtr.html#mallocPlainForeignPtr"><span class="hs-identifier">mallocPlainForeignPtr</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-36"></span><span>        </span><span class="annot"><a href="GHC.ForeignPtr.html#mallocForeignPtrBytes"><span class="hs-identifier">mallocForeignPtrBytes</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-37"></span><span>        </span><span class="annot"><a href="GHC.ForeignPtr.html#mallocPlainForeignPtrBytes"><span class="hs-identifier">mallocPlainForeignPtrBytes</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-38"></span><span>        </span><span class="annot"><a href="GHC.ForeignPtr.html#mallocForeignPtrAlignedBytes"><span class="hs-identifier">mallocForeignPtrAlignedBytes</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-39"></span><span>        </span><span class="annot"><a href="GHC.ForeignPtr.html#mallocPlainForeignPtrAlignedBytes"><span class="hs-identifier">mallocPlainForeignPtrAlignedBytes</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-40"></span><span>        </span><span class="annot"><a href="GHC.ForeignPtr.html#newConcForeignPtr"><span class="hs-identifier">newConcForeignPtr</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-41"></span><span>        </span><span class="annot"><span class="hs-comment">-- * Add Finalizers</span></span><span>
</span><span id="line-42"></span><span>        </span><span class="annot"><a href="GHC.ForeignPtr.html#addForeignPtrFinalizer"><span class="hs-identifier">addForeignPtrFinalizer</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-43"></span><span>        </span><span class="annot"><a href="GHC.ForeignPtr.html#addForeignPtrFinalizerEnv"><span class="hs-identifier">addForeignPtrFinalizerEnv</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-44"></span><span>        </span><span class="annot"><a href="GHC.ForeignPtr.html#addForeignPtrConcFinalizer"><span class="hs-identifier">addForeignPtrConcFinalizer</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-45"></span><span>        </span><span class="annot"><span class="hs-comment">-- * Conversion</span></span><span>
</span><span id="line-46"></span><span>        </span><span class="annot"><a href="GHC.ForeignPtr.html#unsafeForeignPtrToPtr"><span class="hs-identifier">unsafeForeignPtrToPtr</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-47"></span><span>        </span><span class="annot"><a href="GHC.ForeignPtr.html#castForeignPtr"><span class="hs-identifier">castForeignPtr</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-48"></span><span>        </span><span class="annot"><a href="GHC.ForeignPtr.html#plusForeignPtr"><span class="hs-identifier">plusForeignPtr</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-49"></span><span>        </span><span class="annot"><span class="hs-comment">-- * Control over lifetype</span></span><span>
</span><span id="line-50"></span><span>        </span><span class="annot"><a href="GHC.ForeignPtr.html#withForeignPtr"><span class="hs-identifier">withForeignPtr</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-51"></span><span>        </span><span class="annot"><a href="GHC.ForeignPtr.html#unsafeWithForeignPtr"><span class="hs-identifier">unsafeWithForeignPtr</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-52"></span><span>        </span><span class="annot"><a href="GHC.ForeignPtr.html#touchForeignPtr"><span class="hs-identifier">touchForeignPtr</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-53"></span><span>        </span><span class="annot"><span class="hs-comment">-- * Finalization</span></span><span>
</span><span id="line-54"></span><span>        </span><span class="annot"><a href="GHC.ForeignPtr.html#finalizeForeignPtr"><span class="hs-identifier">finalizeForeignPtr</span></a></span><span>
</span><span id="line-55"></span><span>        </span><span class="annot"><span class="hs-comment">-- * Commentary</span></span><span>
</span><span id="line-56"></span><span>        </span><span class="annot"><span class="hs-comment">-- $commentary</span></span><span>
</span><span id="line-57"></span><span>  </span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-58"></span><span>
</span><span id="line-59"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Foreign.Storable.html"><span class="hs-identifier">Foreign.Storable</span></a></span><span>
</span><span id="line-60"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Data.Foldable.html"><span class="hs-identifier">Data.Foldable</span></a></span><span>    </span><span class="hs-special">(</span><span> </span><span class="annot"><a href="Data.Foldable.html#sequence_"><span class="hs-identifier">sequence_</span></a></span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-61"></span><span>
</span><span id="line-62"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Show.html"><span class="hs-identifier">GHC.Show</span></a></span><span>
</span><span id="line-63"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Base.html"><span class="hs-identifier">GHC.Base</span></a></span><span>
</span><span id="line-64"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.IORef.html"><span class="hs-identifier">GHC.IORef</span></a></span><span>
</span><span id="line-65"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.STRef.html"><span class="hs-identifier">GHC.STRef</span></a></span><span>        </span><span class="hs-special">(</span><span> </span><span class="annot"><a href="GHC.STRef.html#STRef"><span class="hs-identifier">STRef</span></a></span><span class="hs-special">(</span><span class="hs-glyph">..</span><span class="hs-special">)</span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-66"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="GHC.Ptr.html"><span class="hs-identifier">GHC.Ptr</span></a></span><span>          </span><span class="hs-special">(</span><span> </span><span class="annot"><a href="GHC.Ptr.html#Ptr"><span class="hs-identifier">Ptr</span></a></span><span class="hs-special">(</span><span class="hs-glyph">..</span><span class="hs-special">)</span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.Ptr.html#FunPtr"><span class="hs-identifier">FunPtr</span></a></span><span class="hs-special">(</span><span class="hs-glyph">..</span><span class="hs-special">)</span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-67"></span><span>
</span><span id="line-68"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Unsafe.Coerce.html"><span class="hs-identifier">Unsafe.Coerce</span></a></span><span>    </span><span class="hs-special">(</span><span> </span><span class="annot"><a href="Unsafe.Coerce.html#unsafeCoerce"><span class="hs-identifier">unsafeCoerce</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Unsafe.Coerce.html#unsafeCoerceUnlifted"><span class="hs-identifier">unsafeCoerceUnlifted</span></a></span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-69"></span><span>
</span><span id="line-70"></span><span class="hs-comment">-- |The type 'ForeignPtr' represents references to objects that are</span><span>
</span><span id="line-71"></span><span class="hs-comment">-- maintained in a foreign language, i.e., that are not part of the</span><span>
</span><span id="line-72"></span><span class="hs-comment">-- data structures usually managed by the Haskell storage manager.</span><span>
</span><span id="line-73"></span><span class="hs-comment">-- The essential difference between 'ForeignPtr's and vanilla memory</span><span>
</span><span id="line-74"></span><span class="hs-comment">-- references of type @Ptr a@ is that the former may be associated</span><span>
</span><span id="line-75"></span><span class="hs-comment">-- with /finalizers/. A finalizer is a routine that is invoked when</span><span>
</span><span id="line-76"></span><span class="hs-comment">-- the Haskell storage manager detects that - within the Haskell heap</span><span>
</span><span id="line-77"></span><span class="hs-comment">-- and stack - there are no more references left that are pointing to</span><span>
</span><span id="line-78"></span><span class="hs-comment">-- the 'ForeignPtr'.  Typically, the finalizer will, then, invoke</span><span>
</span><span id="line-79"></span><span class="hs-comment">-- routines in the foreign language that free the resources bound by</span><span>
</span><span id="line-80"></span><span class="hs-comment">-- the foreign object.</span><span>
</span><span id="line-81"></span><span class="hs-comment">--</span><span>
</span><span id="line-82"></span><span class="hs-comment">-- The 'ForeignPtr' is parameterised in the same way as 'Ptr'.  The</span><span>
</span><span id="line-83"></span><span class="hs-comment">-- type argument of 'ForeignPtr' should normally be an instance of</span><span>
</span><span id="line-84"></span><span class="hs-comment">-- class 'Storable'.</span><span>
</span><span id="line-85"></span><span class="hs-comment">--</span><span>
</span><span id="line-86"></span><span class="hs-keyword">data</span><span> </span><span id="ForeignPtr"><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-var">ForeignPtr</span></a></span></span><span> </span><span id="local-6989586621679534914"><span class="annot"><a href="#local-6989586621679534914"><span class="hs-identifier hs-type">a</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span id="ForeignPtr"><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-var">ForeignPtr</span></a></span></span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Prim.html#Addr%23"><span class="hs-identifier hs-type">Addr#</span></a></span><span> </span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtrContents"><span class="hs-identifier hs-type">ForeignPtrContents</span></a></span><span>
</span><span id="line-87"></span><span>        </span><span class="hs-comment">-- The Addr# in the ForeignPtr object is intentionally stored</span><span>
</span><span id="line-88"></span><span>        </span><span class="hs-comment">-- separately from the finalizer. The primary aim of the</span><span>
</span><span id="line-89"></span><span>        </span><span class="hs-comment">-- representation is to make withForeignPtr efficient; in fact,</span><span>
</span><span id="line-90"></span><span>        </span><span class="hs-comment">-- withForeignPtr should be just as efficient as unpacking a</span><span>
</span><span id="line-91"></span><span>        </span><span class="hs-comment">-- Ptr, and multiple withForeignPtrs can share an unpacked</span><span>
</span><span id="line-92"></span><span>        </span><span class="hs-comment">-- ForeignPtr. As a secondary benefit, this representation</span><span>
</span><span id="line-93"></span><span>        </span><span class="hs-comment">-- allows pointers to subregions within the same overall block</span><span>
</span><span id="line-94"></span><span>        </span><span class="hs-comment">-- to share the same finalizer (see 'plusForeignPtr'). Note</span><span>
</span><span id="line-95"></span><span>        </span><span class="hs-comment">-- that touchForeignPtr only has to touch the ForeignPtrContents</span><span>
</span><span id="line-96"></span><span>        </span><span class="hs-comment">-- object, because that ensures that whatever the finalizer is</span><span>
</span><span id="line-97"></span><span>        </span><span class="hs-comment">-- attached to is kept alive.</span><span>
</span><span id="line-98"></span><span>
</span><span id="line-99"></span><span class="hs-comment">-- | Functions called when a 'ForeignPtr' is finalized. Note that</span><span>
</span><span id="line-100"></span><span class="hs-comment">-- C finalizers and Haskell finalizers cannot be mixed.</span><span>
</span><span id="line-101"></span><span class="hs-keyword">data</span><span> </span><span id="Finalizers"><span class="annot"><a href="GHC.ForeignPtr.html#Finalizers"><span class="hs-identifier hs-var">Finalizers</span></a></span></span><span>
</span><span id="line-102"></span><span>  </span><span class="hs-glyph">=</span><span> </span><span id="NoFinalizers"><span class="annot"><a href="GHC.ForeignPtr.html#NoFinalizers"><span class="hs-identifier hs-var">NoFinalizers</span></a></span></span><span>
</span><span id="line-103"></span><span>    </span><span class="hs-comment">-- ^ No finalizer. If there is no intent to add a finalizer at</span><span>
</span><span id="line-104"></span><span>    </span><span class="hs-comment">-- any point in the future, consider 'FinalPtr' or 'PlainPtr' instead</span><span>
</span><span id="line-105"></span><span>    </span><span class="hs-comment">-- since these perform fewer allocations.</span><span>
</span><span id="line-106"></span><span>  </span><span class="hs-glyph">|</span><span> </span><span id="CFinalizers"><span class="annot"><a href="GHC.ForeignPtr.html#CFinalizers"><span class="hs-identifier hs-var">CFinalizers</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="../../ghc-prim/src/GHC.Prim.html#Weak%23"><span class="hs-identifier hs-type">Weak#</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-107"></span><span>    </span><span class="hs-comment">-- ^ Finalizers are all C functions.</span><span>
</span><span id="line-108"></span><span>  </span><span class="hs-glyph">|</span><span> </span><span id="HaskellFinalizers"><span class="annot"><a href="GHC.ForeignPtr.html#HaskellFinalizers"><span class="hs-identifier hs-var">HaskellFinalizers</span></a></span></span><span> </span><span class="hs-special">[</span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span class="hs-special">]</span><span>
</span><span id="line-109"></span><span>    </span><span class="hs-comment">-- ^ Finalizers are all Haskell functions.</span><span>
</span><span id="line-110"></span><span>
</span><span id="line-111"></span><span class="hs-comment">-- | Controls finalization of a 'ForeignPtr', that is, what should happen</span><span>
</span><span id="line-112"></span><span class="hs-comment">-- if the 'ForeignPtr' becomes unreachable. Visually, these data constructors</span><span>
</span><span id="line-113"></span><span class="hs-comment">-- are appropriate in these scenarios:</span><span>
</span><span id="line-114"></span><span class="hs-comment">--</span><span>
</span><span id="line-115"></span><span class="hs-comment">-- &gt;                           Memory backing pointer is</span><span>
</span><span id="line-116"></span><span class="hs-comment">-- &gt;                            GC-Managed   Unmanaged</span><span>
</span><span id="line-117"></span><span class="hs-comment">-- &gt; Finalizer functions are: +------------+-----------------+</span><span>
</span><span id="line-118"></span><span class="hs-comment">-- &gt;                 Allowed  | MallocPtr  | PlainForeignPtr |</span><span>
</span><span id="line-119"></span><span class="hs-comment">-- &gt;                          +------------+-----------------+</span><span>
</span><span id="line-120"></span><span class="hs-comment">-- &gt;              Prohibited  | PlainPtr   | FinalPtr        |</span><span>
</span><span id="line-121"></span><span class="hs-comment">-- &gt;                          +------------+-----------------+</span><span>
</span><span id="line-122"></span><span class="hs-keyword">data</span><span> </span><span id="ForeignPtrContents"><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtrContents"><span class="hs-identifier hs-var">ForeignPtrContents</span></a></span></span><span>
</span><span id="line-123"></span><span>  </span><span class="hs-glyph">=</span><span> </span><span id="PlainForeignPtr"><span class="annot"><a href="GHC.ForeignPtr.html#PlainForeignPtr"><span class="hs-identifier hs-var">PlainForeignPtr</span></a></span></span><span> </span><span class="hs-glyph">!</span><span class="hs-special">(</span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span class="annot"><a href="GHC.ForeignPtr.html#Finalizers"><span class="hs-identifier hs-type">Finalizers</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-124"></span><span>    </span><span class="hs-comment">-- ^ The pointer refers to unmanaged memory that was allocated by</span><span>
</span><span id="line-125"></span><span>    </span><span class="hs-comment">-- a foreign function (typically using @malloc@). The finalizer</span><span>
</span><span id="line-126"></span><span>    </span><span class="hs-comment">-- frequently calls the C function @free@ or some variant of it.</span><span>
</span><span id="line-127"></span><span>  </span><span class="hs-glyph">|</span><span> </span><span id="FinalPtr"><span class="annot"><a href="GHC.ForeignPtr.html#FinalPtr"><span class="hs-identifier hs-var">FinalPtr</span></a></span></span><span>
</span><span id="line-128"></span><span>    </span><span class="hs-comment">-- ^ The pointer refers to unmanaged memory that should not be freed when</span><span>
</span><span id="line-129"></span><span>    </span><span class="hs-comment">-- the 'ForeignPtr' becomes unreachable. Functions that add finalizers</span><span>
</span><span id="line-130"></span><span>    </span><span class="hs-comment">-- to a 'ForeignPtr' throw exceptions when the 'ForeignPtr' is backed by</span><span>
</span><span id="line-131"></span><span>    </span><span class="hs-comment">-- 'PlainPtr'Most commonly, this is used with @Addr#@ literals.</span><span>
</span><span id="line-132"></span><span>    </span><span class="hs-comment">-- See Note [Why FinalPtr].</span><span>
</span><span id="line-133"></span><span>    </span><span class="hs-comment">--</span><span>
</span><span id="line-134"></span><span>    </span><span class="hs-comment">-- @since 4.15</span><span>
</span><span id="line-135"></span><span>  </span><span class="hs-glyph">|</span><span> </span><span id="MallocPtr"><span class="annot"><a href="GHC.ForeignPtr.html#MallocPtr"><span class="hs-identifier hs-var">MallocPtr</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="../../ghc-prim/src/GHC.Prim.html#MutableByteArray%23"><span class="hs-identifier hs-type">MutableByteArray#</span></a></span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Prim.html#RealWorld"><span class="hs-identifier hs-type">RealWorld</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">!</span><span class="hs-special">(</span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span class="annot"><a href="GHC.ForeignPtr.html#Finalizers"><span class="hs-identifier hs-type">Finalizers</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-136"></span><span>    </span><span class="hs-comment">-- ^ The pointer refers to a byte array.</span><span>
</span><span id="line-137"></span><span>    </span><span class="hs-comment">-- The 'MutableByteArray#' field means that the 'MutableByteArray#' is</span><span>
</span><span id="line-138"></span><span>    </span><span class="hs-comment">-- reachable (by GC) whenever the 'ForeignPtr' is reachable. When the</span><span>
</span><span id="line-139"></span><span>    </span><span class="hs-comment">-- 'ForeignPtr' becomes unreachable, the runtime\'s normal GC recovers</span><span>
</span><span id="line-140"></span><span>    </span><span class="hs-comment">-- the memory backing it. Here, the finalizer function intended to be used</span><span>
</span><span id="line-141"></span><span>    </span><span class="hs-comment">-- to @free()@ any ancilliary *unmanaged* memory pointed to by the</span><span>
</span><span id="line-142"></span><span>    </span><span class="hs-comment">-- 'MutableByteArray#'. See the @zlib@ library for an example of this use.</span><span>
</span><span id="line-143"></span><span>    </span><span class="hs-comment">--</span><span>
</span><span id="line-144"></span><span>    </span><span class="hs-comment">-- 1. Invariant: The 'Addr#' in the parent 'ForeignPtr' is an interior</span><span>
</span><span id="line-145"></span><span>    </span><span class="hs-comment">--    pointer into this 'MutableByteArray#'.</span><span>
</span><span id="line-146"></span><span>    </span><span class="hs-comment">-- 2. Invariant: The 'MutableByteArray#' is pinned, so the 'Addr#' does not</span><span>
</span><span id="line-147"></span><span>    </span><span class="hs-comment">--    get invalidated by the GC moving the byte array.</span><span>
</span><span id="line-148"></span><span>    </span><span class="hs-comment">-- 3. Invariant: A 'MutableByteArray#' must not be associated with more than</span><span>
</span><span id="line-149"></span><span>    </span><span class="hs-comment">--    one set of finalizers. For example, this is sound:</span><span>
</span><span id="line-150"></span><span>    </span><span class="hs-comment">--</span><span>
</span><span id="line-151"></span><span>    </span><span class="hs-comment">--    &gt; incrGood :: ForeignPtr Word8 -&gt; ForeignPtr Word8</span><span>
</span><span id="line-152"></span><span>    </span><span class="hs-comment">--    &gt; incrGood (ForeignPtr p (MallocPtr m f)) = ForeignPtr (plusPtr p 1) (MallocPtr m f)</span><span>
</span><span id="line-153"></span><span>    </span><span class="hs-comment">--</span><span>
</span><span id="line-154"></span><span>    </span><span class="hs-comment">--    But this is unsound:</span><span>
</span><span id="line-155"></span><span>    </span><span class="hs-comment">--</span><span>
</span><span id="line-156"></span><span>    </span><span class="hs-comment">--    &gt; incrBad :: ForeignPtr Word8 -&gt; IO (ForeignPtr Word8)</span><span>
</span><span id="line-157"></span><span>    </span><span class="hs-comment">--    &gt; incrBad (ForeignPtr p (MallocPtr m _)) = do</span><span>
</span><span id="line-158"></span><span>    </span><span class="hs-comment">--    &gt;   f &lt;- newIORef NoFinalizers</span><span>
</span><span id="line-159"></span><span>    </span><span class="hs-comment">--    &gt;   pure (ForeignPtr p (MallocPtr m f))</span><span>
</span><span id="line-160"></span><span>  </span><span class="hs-glyph">|</span><span> </span><span id="PlainPtr"><span class="annot"><a href="GHC.ForeignPtr.html#PlainPtr"><span class="hs-identifier hs-var">PlainPtr</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="../../ghc-prim/src/GHC.Prim.html#MutableByteArray%23"><span class="hs-identifier hs-type">MutableByteArray#</span></a></span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Prim.html#RealWorld"><span class="hs-identifier hs-type">RealWorld</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-161"></span><span>    </span><span class="hs-comment">-- ^ The pointer refers to a byte array. Finalization is not</span><span>
</span><span id="line-162"></span><span>    </span><span class="hs-comment">-- supported. This optimizes @MallocPtr@ by avoiding the allocation</span><span>
</span><span id="line-163"></span><span>    </span><span class="hs-comment">-- of a @MutVar#@ when it is known that no one will add finalizers to</span><span>
</span><span id="line-164"></span><span>    </span><span class="hs-comment">-- the @ForeignPtr@. Functions that add finalizers to a 'ForeignPtr'</span><span>
</span><span id="line-165"></span><span>    </span><span class="hs-comment">-- throw exceptions when the 'ForeignPtr' is backed by 'PlainPtr'.</span><span>
</span><span id="line-166"></span><span>    </span><span class="hs-comment">-- The invariants that apply to 'MallocPtr' apply to 'PlainPtr' as well.</span><span>
</span><span id="line-167"></span><span>
</span><span id="line-168"></span><span class="hs-comment">-- Note [Why FinalPtr]</span><span>
</span><span id="line-169"></span><span class="hs-comment">--</span><span>
</span><span id="line-170"></span><span class="hs-comment">-- FinalPtr exists as an optimization for foreign pointers created</span><span>
</span><span id="line-171"></span><span class="hs-comment">-- from Addr# literals. Most commonly, this happens in the bytestring</span><span>
</span><span id="line-172"></span><span class="hs-comment">-- library, where the combination of OverloadedStrings and a rewrite</span><span>
</span><span id="line-173"></span><span class="hs-comment">-- rule overloads String literals as ByteString literals. See the</span><span>
</span><span id="line-174"></span><span class="hs-comment">-- rule &quot;ByteString packChars/packAddress&quot; in</span><span>
</span><span id="line-175"></span><span class="hs-comment">-- bytestring:Data.ByteString.Internal. Prior to the</span><span>
</span><span id="line-176"></span><span class="hs-comment">-- introduction of FinalPtr, bytestring used PlainForeignPtr (in</span><span>
</span><span id="line-177"></span><span class="hs-comment">-- Data.ByteString.Internal.unsafePackAddress) to handle such literals.</span><span>
</span><span id="line-178"></span><span class="hs-comment">-- With O2 optimization, the resulting Core from a GHC patched with a</span><span>
</span><span id="line-179"></span><span class="hs-comment">-- known-key cstringLength# function but without FinalPtr looked like:</span><span>
</span><span id="line-180"></span><span class="hs-comment">--</span><span>
</span><span id="line-181"></span><span class="hs-comment">--   RHS size: {terms: 1, types: 0, coercions: 0, joins: 0/0}</span><span>
</span><span id="line-182"></span><span class="hs-comment">--   stringOne1 = &quot;hello beautiful world&quot;#</span><span>
</span><span id="line-183"></span><span class="hs-comment">--   RHS size: {terms: 11, types: 17, coercions: 0, joins: 0/0}</span><span>
</span><span id="line-184"></span><span class="hs-comment">--   stringOne</span><span>
</span><span id="line-185"></span><span class="hs-comment">--     = case newMutVar# NoFinalizers realWorld# of</span><span>
</span><span id="line-186"></span><span class="hs-comment">--       { (# ipv_i7b6, ipv1_i7b7 #) -&gt;</span><span>
</span><span id="line-187"></span><span class="hs-comment">--       PS stringOne1 (PlainForeignPtr ipv1_i7b7) 0# 21#</span><span>
</span><span id="line-188"></span><span class="hs-comment">--       }</span><span>
</span><span id="line-189"></span><span class="hs-comment">--</span><span>
</span><span id="line-190"></span><span class="hs-comment">-- After the introduction of FinalPtr, the bytestring library was modified</span><span>
</span><span id="line-191"></span><span class="hs-comment">-- so that the resulting Core was instead:</span><span>
</span><span id="line-192"></span><span class="hs-comment">--</span><span>
</span><span id="line-193"></span><span class="hs-comment">--   RHS size: {terms: 1, types: 0, coercions: 0, joins: 0/0}</span><span>
</span><span id="line-194"></span><span class="hs-comment">--   stringOne1 = &quot;hello beautiful world&quot;#</span><span>
</span><span id="line-195"></span><span class="hs-comment">--   RHS size: {terms: 5, types: 0, coercions: 0, joins: 0/0}</span><span>
</span><span id="line-196"></span><span class="hs-comment">--   stringOne = PS stringOne1 FinalPtr 0# 21#</span><span>
</span><span id="line-197"></span><span class="hs-comment">--</span><span>
</span><span id="line-198"></span><span class="hs-comment">-- This improves performance in three ways:</span><span>
</span><span id="line-199"></span><span class="hs-comment">--</span><span>
</span><span id="line-200"></span><span class="hs-comment">-- 1. More optimization opportunities. GHC is willing to inline the FinalPtr</span><span>
</span><span id="line-201"></span><span class="hs-comment">--    variant of stringOne into its use sites. This means the offset and length</span><span>
</span><span id="line-202"></span><span class="hs-comment">--    are eligible for case-of-known-literal. Previously, this never happened.</span><span>
</span><span id="line-203"></span><span class="hs-comment">-- 2. Smaller binaries. Setting up the thunk to call newMutVar# required</span><span>
</span><span id="line-204"></span><span class="hs-comment">--    machine instruction in the generated code. On x86_64, FinalPtr reduces</span><span>
</span><span id="line-205"></span><span class="hs-comment">--    the size of binaries by about 450 bytes per ByteString literal.</span><span>
</span><span id="line-206"></span><span class="hs-comment">-- 3. Smaller memory footprint. Previously, every ByteString literal resulted</span><span>
</span><span id="line-207"></span><span class="hs-comment">--    in the allocation of a MutVar# and a PlainForeignPtr data constructor.</span><span>
</span><span id="line-208"></span><span class="hs-comment">--    These both hang around until the ByteString goes out of scope. FinalPtr</span><span>
</span><span id="line-209"></span><span class="hs-comment">--    eliminates both of these sources of allocations. The MutVar# is not</span><span>
</span><span id="line-210"></span><span class="hs-comment">--    allocated because FinalPtr does not allow it, and the data constructor</span><span>
</span><span id="line-211"></span><span class="hs-comment">--    is not allocated because FinalPtr is a nullary data constructor.</span><span>
</span><span id="line-212"></span><span class="hs-comment">--</span><span>
</span><span id="line-213"></span><span class="hs-comment">-- For more discussion of FinalPtr, see GHC MR #2165 and bytestring PR #191.</span><span>
</span><span id="line-214"></span><span>
</span><span id="line-215"></span><span class="hs-comment">-- | @since 2.01</span><span>
</span><span id="line-216"></span><span id="local-6989586621679534949"><span class="hs-keyword">instance</span><span> </span><span id="local-6989586621679534790"><span class="annot"><a href="../../ghc-prim/src/GHC.Classes.html#Eq"><span class="hs-identifier hs-type">Eq</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534949"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span></span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-217"></span><span>    </span><span id="local-6989586621679534786"><span class="annot"><span class="annottext">ForeignPtr a
</span><a href="#local-6989586621679534786"><span class="hs-identifier hs-var">p</span></a></span></span><span> </span><span id="local-6989586621679534785"><span class="annot"><span class="annottext">== :: ForeignPtr a -&gt; ForeignPtr a -&gt; Bool
</span><a href="../../ghc-prim/src/GHC.Classes.html#%3D%3D"><span class="hs-operator hs-var hs-var hs-var hs-var">==</span></a></span></span><span> </span><span id="local-6989586621679534784"><span class="annot"><span class="annottext">ForeignPtr a
</span><a href="#local-6989586621679534784"><span class="hs-identifier hs-var">q</span></a></span></span><span>  </span><span class="hs-glyph">=</span><span>  </span><span class="annot"><span class="annottext">ForeignPtr a -&gt; Ptr a
forall a. ForeignPtr a -&gt; Ptr a
</span><a href="GHC.ForeignPtr.html#unsafeForeignPtrToPtr"><span class="hs-identifier hs-var">unsafeForeignPtrToPtr</span></a></span><span> </span><span class="annot"><span class="annottext">ForeignPtr a
</span><a href="#local-6989586621679534786"><span class="hs-identifier hs-var">p</span></a></span><span> </span><span class="annot"><span class="annottext">Ptr a -&gt; Ptr a -&gt; Bool
forall a. Eq a =&gt; a -&gt; a -&gt; Bool
</span><a href="../../ghc-prim/src/GHC.Classes.html#%3D%3D"><span class="hs-operator hs-var">==</span></a></span><span> </span><span class="annot"><span class="annottext">ForeignPtr a -&gt; Ptr a
forall a. ForeignPtr a -&gt; Ptr a
</span><a href="GHC.ForeignPtr.html#unsafeForeignPtrToPtr"><span class="hs-identifier hs-var">unsafeForeignPtrToPtr</span></a></span><span> </span><span class="annot"><span class="annottext">ForeignPtr a
</span><a href="#local-6989586621679534784"><span class="hs-identifier hs-var">q</span></a></span></span><span>
</span><span id="line-218"></span><span>
</span><span id="line-219"></span><span class="hs-comment">-- | @since 2.01</span><span>
</span><span id="line-220"></span><span id="local-6989586621679534945"><span class="hs-keyword">instance</span><span> </span><span id="local-6989586621679534769"><span id="local-6989586621679534771"><span id="local-6989586621679534773"><span id="local-6989586621679534775"><span id="local-6989586621679534777"><span id="local-6989586621679534779"><span class="annot"><a href="../../ghc-prim/src/GHC.Classes.html#Ord"><span class="hs-identifier hs-type">Ord</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534945"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span></span></span></span></span></span></span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-221"></span><span>    </span><span id="local-6989586621679534765"><span class="annot"><span class="annottext">compare :: ForeignPtr a -&gt; ForeignPtr a -&gt; Ordering
</span><a href="../../ghc-prim/src/GHC.Classes.html#compare"><span class="hs-identifier hs-var hs-var hs-var hs-var">compare</span></a></span></span><span> </span><span id="local-6989586621679534763"><span class="annot"><span class="annottext">ForeignPtr a
</span><a href="#local-6989586621679534763"><span class="hs-identifier hs-var">p</span></a></span></span><span> </span><span id="local-6989586621679534762"><span class="annot"><span class="annottext">ForeignPtr a
</span><a href="#local-6989586621679534762"><span class="hs-identifier hs-var">q</span></a></span></span><span>  </span><span class="hs-glyph">=</span><span>  </span><span class="annot"><span class="annottext">Ptr a -&gt; Ptr a -&gt; Ordering
forall a. Ord a =&gt; a -&gt; a -&gt; Ordering
</span><a href="../../ghc-prim/src/GHC.Classes.html#compare"><span class="hs-identifier hs-var">compare</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">ForeignPtr a -&gt; Ptr a
forall a. ForeignPtr a -&gt; Ptr a
</span><a href="GHC.ForeignPtr.html#unsafeForeignPtrToPtr"><span class="hs-identifier hs-var">unsafeForeignPtrToPtr</span></a></span><span> </span><span class="annot"><span class="annottext">ForeignPtr a
</span><a href="#local-6989586621679534763"><span class="hs-identifier hs-var">p</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">ForeignPtr a -&gt; Ptr a
forall a. ForeignPtr a -&gt; Ptr a
</span><a href="GHC.ForeignPtr.html#unsafeForeignPtrToPtr"><span class="hs-identifier hs-var">unsafeForeignPtrToPtr</span></a></span><span> </span><span class="annot"><span class="annottext">ForeignPtr a
</span><a href="#local-6989586621679534762"><span class="hs-identifier hs-var">q</span></a></span><span class="hs-special">)</span></span><span>
</span><span id="line-222"></span><span>
</span><span id="line-223"></span><span class="hs-comment">-- | @since 2.01</span><span>
</span><span id="line-224"></span><span id="local-6989586621679534941"><span class="hs-keyword">instance</span><span> </span><span id="local-6989586621679534757"><span id="local-6989586621679534759"><span class="annot"><a href="GHC.Show.html#Show"><span class="hs-identifier hs-type">Show</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534941"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span></span></span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-225"></span><span>    </span><span id="local-6989586621679534753"><span class="annot"><span class="annottext">showsPrec :: Int -&gt; ForeignPtr a -&gt; ShowS
</span><a href="GHC.Show.html#showsPrec"><span class="hs-identifier hs-var hs-var hs-var hs-var">showsPrec</span></a></span></span><span> </span><span id="local-6989586621679534751"><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679534751"><span class="hs-identifier hs-var">p</span></a></span></span><span> </span><span id="local-6989586621679534750"><span class="annot"><span class="annottext">ForeignPtr a
</span><a href="#local-6989586621679534750"><span class="hs-identifier hs-var">f</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Int -&gt; Ptr a -&gt; ShowS
forall a. Show a =&gt; Int -&gt; a -&gt; ShowS
</span><a href="GHC.Show.html#showsPrec"><span class="hs-identifier hs-var">showsPrec</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679534751"><span class="hs-identifier hs-var">p</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">ForeignPtr a -&gt; Ptr a
forall a. ForeignPtr a -&gt; Ptr a
</span><a href="GHC.ForeignPtr.html#unsafeForeignPtrToPtr"><span class="hs-identifier hs-var">unsafeForeignPtrToPtr</span></a></span><span> </span><span class="annot"><span class="annottext">ForeignPtr a
</span><a href="#local-6989586621679534750"><span class="hs-identifier hs-var">f</span></a></span><span class="hs-special">)</span></span><span>
</span><span id="line-226"></span><span>
</span><span id="line-227"></span><span>
</span><span id="line-228"></span><span class="hs-comment">-- |A finalizer is represented as a pointer to a foreign function that, at</span><span>
</span><span id="line-229"></span><span class="hs-comment">-- finalisation time, gets as an argument a plain pointer variant of the</span><span>
</span><span id="line-230"></span><span class="hs-comment">-- foreign pointer that the finalizer is associated with.</span><span>
</span><span id="line-231"></span><span class="hs-comment">--</span><span>
</span><span id="line-232"></span><span class="hs-comment">-- Note that the foreign function /must/ use the @ccall@ calling convention.</span><span>
</span><span id="line-233"></span><span class="hs-comment">--</span><span>
</span><span id="line-234"></span><span class="hs-keyword">type</span><span> </span><span id="FinalizerPtr"><span class="annot"><a href="GHC.ForeignPtr.html#FinalizerPtr"><span class="hs-identifier hs-var">FinalizerPtr</span></a></span></span><span> </span><span id="local-6989586621679534749"><span class="annot"><a href="#local-6989586621679534749"><span class="hs-identifier hs-type">a</span></a></span></span><span>        </span><span class="hs-glyph">=</span><span> </span><span class="annot"><a href="GHC.Ptr.html#FunPtr"><span class="hs-identifier hs-type">FunPtr</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Ptr.html#Ptr"><span class="hs-identifier hs-type">Ptr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534749"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-235"></span><span class="hs-keyword">type</span><span> </span><span id="FinalizerEnvPtr"><span class="annot"><a href="GHC.ForeignPtr.html#FinalizerEnvPtr"><span class="hs-identifier hs-var">FinalizerEnvPtr</span></a></span></span><span> </span><span id="local-6989586621679534748"><span class="annot"><a href="#local-6989586621679534748"><span class="hs-identifier hs-type">env</span></a></span></span><span> </span><span id="local-6989586621679534747"><span class="annot"><a href="#local-6989586621679534747"><span class="hs-identifier hs-type">a</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><a href="GHC.Ptr.html#FunPtr"><span class="hs-identifier hs-type">FunPtr</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Ptr.html#Ptr"><span class="hs-identifier hs-type">Ptr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534748"><span class="hs-identifier hs-type">env</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Ptr.html#Ptr"><span class="hs-identifier hs-type">Ptr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534747"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-236"></span><span>
</span><span id="line-237"></span><span id="local-6989586621679534939"><span class="annot"><a href="GHC.ForeignPtr.html#newConcForeignPtr"><span class="hs-identifier hs-type">newConcForeignPtr</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.Ptr.html#Ptr"><span class="hs-identifier hs-type">Ptr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534939"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534939"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span></span><span>
</span><span id="line-238"></span><span class="hs-comment">--</span><span>
</span><span id="line-239"></span><span class="hs-comment">-- ^Turns a plain memory reference into a foreign object by</span><span>
</span><span id="line-240"></span><span class="hs-comment">-- associating a finalizer - given by the monadic operation - with the</span><span>
</span><span id="line-241"></span><span class="hs-comment">-- reference.  The storage manager will start the finalizer, in a</span><span>
</span><span id="line-242"></span><span class="hs-comment">-- separate thread, some time after the last reference to the</span><span>
</span><span id="line-243"></span><span class="hs-comment">-- @ForeignPtr@ is dropped.  There is no guarantee of promptness, and</span><span>
</span><span id="line-244"></span><span class="hs-comment">-- in fact there is no guarantee that the finalizer will eventually</span><span>
</span><span id="line-245"></span><span class="hs-comment">-- run at all.</span><span>
</span><span id="line-246"></span><span class="hs-comment">--</span><span>
</span><span id="line-247"></span><span class="hs-comment">-- Note that references from a finalizer do not necessarily prevent</span><span>
</span><span id="line-248"></span><span class="hs-comment">-- another object from being finalized.  If A's finalizer refers to B</span><span>
</span><span id="line-249"></span><span class="hs-comment">-- (perhaps using 'touchForeignPtr', then the only guarantee is that</span><span>
</span><span id="line-250"></span><span class="hs-comment">-- B's finalizer will never be started before A's.  If both A and B</span><span>
</span><span id="line-251"></span><span class="hs-comment">-- are unreachable, then both finalizers will start together.  See</span><span>
</span><span id="line-252"></span><span class="hs-comment">-- 'touchForeignPtr' for more on finalizer ordering.</span><span>
</span><span id="line-253"></span><span class="hs-comment">--</span><span>
</span><span id="line-254"></span><span id="newConcForeignPtr"><span class="annot"><span class="annottext">newConcForeignPtr :: forall a. Ptr a -&gt; IO () -&gt; IO (ForeignPtr a)
</span><a href="GHC.ForeignPtr.html#newConcForeignPtr"><span class="hs-identifier hs-var hs-var">newConcForeignPtr</span></a></span></span><span> </span><span id="local-6989586621679534743"><span class="annot"><span class="annottext">Ptr a
</span><a href="#local-6989586621679534743"><span class="hs-identifier hs-var">p</span></a></span></span><span> </span><span id="local-6989586621679534742"><span class="annot"><span class="annottext">IO ()
</span><a href="#local-6989586621679534742"><span class="hs-identifier hs-var">finalizer</span></a></span></span><span>
</span><span id="line-255"></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span> </span><span id="local-6989586621679534741"><span class="annot"><span class="annottext">ForeignPtr a
</span><a href="#local-6989586621679534741"><span class="hs-identifier hs-var">fObj</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">Ptr a -&gt; IO (ForeignPtr a)
forall a. Ptr a -&gt; IO (ForeignPtr a)
</span><a href="GHC.ForeignPtr.html#newForeignPtr_"><span class="hs-identifier hs-var">newForeignPtr_</span></a></span><span> </span><span class="annot"><span class="annottext">Ptr a
</span><a href="#local-6989586621679534743"><span class="hs-identifier hs-var">p</span></a></span><span>
</span><span id="line-256"></span><span>       </span><span class="annot"><span class="annottext">ForeignPtr a -&gt; IO () -&gt; IO ()
forall a. ForeignPtr a -&gt; IO () -&gt; IO ()
</span><a href="GHC.ForeignPtr.html#addForeignPtrConcFinalizer"><span class="hs-identifier hs-var">addForeignPtrConcFinalizer</span></a></span><span> </span><span class="annot"><span class="annottext">ForeignPtr a
</span><a href="#local-6989586621679534741"><span class="hs-identifier hs-var">fObj</span></a></span><span> </span><span class="annot"><span class="annottext">IO ()
</span><a href="#local-6989586621679534742"><span class="hs-identifier hs-var">finalizer</span></a></span><span>
</span><span id="line-257"></span><span>       </span><span class="annot"><span class="annottext">ForeignPtr a -&gt; IO (ForeignPtr a)
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="annot"><span class="annottext">ForeignPtr a
</span><a href="#local-6989586621679534741"><span class="hs-identifier hs-var">fObj</span></a></span><span>
</span><span id="line-258"></span><span>
</span><span id="line-259"></span><span id="local-6989586621679534933"><span class="annot"><a href="GHC.ForeignPtr.html#mallocForeignPtr"><span class="hs-identifier hs-type">mallocForeignPtr</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="Foreign.Storable.html#Storable"><span class="hs-identifier hs-type">Storable</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534933"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534933"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span></span><span>
</span><span id="line-260"></span><span class="hs-comment">-- ^ Allocate some memory and return a 'ForeignPtr' to it.  The memory</span><span>
</span><span id="line-261"></span><span class="hs-comment">-- will be released automatically when the 'ForeignPtr' is discarded.</span><span>
</span><span id="line-262"></span><span class="hs-comment">--</span><span>
</span><span id="line-263"></span><span class="hs-comment">-- 'mallocForeignPtr' is equivalent to</span><span>
</span><span id="line-264"></span><span class="hs-comment">--</span><span>
</span><span id="line-265"></span><span class="hs-comment">-- &gt;    do { p &lt;- malloc; newForeignPtr finalizerFree p }</span><span>
</span><span id="line-266"></span><span class="hs-comment">--</span><span>
</span><span id="line-267"></span><span class="hs-comment">-- although it may be implemented differently internally: you may not</span><span>
</span><span id="line-268"></span><span class="hs-comment">-- assume that the memory returned by 'mallocForeignPtr' has been</span><span>
</span><span id="line-269"></span><span class="hs-comment">-- allocated with 'Foreign.Marshal.Alloc.malloc'.</span><span>
</span><span id="line-270"></span><span class="hs-comment">--</span><span>
</span><span id="line-271"></span><span class="hs-comment">-- GHC notes: 'mallocForeignPtr' has a heavily optimised</span><span>
</span><span id="line-272"></span><span class="hs-comment">-- implementation in GHC.  It uses pinned memory in the garbage</span><span>
</span><span id="line-273"></span><span class="hs-comment">-- collected heap, so the 'ForeignPtr' does not require a finalizer to</span><span>
</span><span id="line-274"></span><span class="hs-comment">-- free the memory.  Use of 'mallocForeignPtr' and associated</span><span>
</span><span id="line-275"></span><span class="hs-comment">-- functions is strongly recommended in preference to</span><span>
</span><span id="line-276"></span><span class="hs-comment">-- 'Foreign.ForeignPtr.newForeignPtr' with a finalizer.</span><span>
</span><span id="line-277"></span><span class="hs-comment">--</span><span>
</span><span id="line-278"></span><span id="mallocForeignPtr"><span class="annot"><span class="annottext">mallocForeignPtr :: forall a. Storable a =&gt; IO (ForeignPtr a)
</span><a href="GHC.ForeignPtr.html#mallocForeignPtr"><span class="hs-identifier hs-var hs-var">mallocForeignPtr</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">a -&gt; IO (ForeignPtr a)
forall b. Storable b =&gt; b -&gt; IO (ForeignPtr b)
</span><a href="#local-6989586621679534736"><span class="hs-identifier hs-var">doMalloc</span></a></span><span> </span><span class="annot"><span class="annottext">a
forall a. HasCallStack =&gt; a
</span><a href="GHC.Err.html#undefined"><span class="hs-identifier hs-var">undefined</span></a></span><span>
</span><span id="line-279"></span><span>  </span><span class="hs-keyword">where</span><span> </span><span id="local-6989586621679534929"><span class="annot"><a href="#local-6989586621679534736"><span class="hs-identifier hs-type">doMalloc</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="Foreign.Storable.html#Storable"><span class="hs-identifier hs-type">Storable</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534929"><span class="hs-identifier hs-type">b</span></a></span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679534929"><span class="hs-identifier hs-type">b</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534929"><span class="hs-identifier hs-type">b</span></a></span><span class="hs-special">)</span></span><span>
</span><span id="line-280"></span><span>        </span><span id="local-6989586621679534736"><span class="annot"><span class="annottext">doMalloc :: forall b. Storable b =&gt; b -&gt; IO (ForeignPtr b)
</span><a href="#local-6989586621679534736"><span class="hs-identifier hs-var hs-var">doMalloc</span></a></span></span><span> </span><span id="local-6989586621679534728"><span class="annot"><span class="annottext">b
</span><a href="#local-6989586621679534728"><span class="hs-identifier hs-var">a</span></a></span></span><span>
</span><span id="line-281"></span><span>          </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Int# -&gt; Int
</span><a href="../../ghc-prim/src/GHC.Types.html#I%23"><span class="hs-identifier hs-var">I#</span></a></span><span> </span><span class="annot"><span class="annottext">Int#
</span><a href="#local-6989586621679534727"><span class="hs-identifier hs-var">size</span></a></span><span> </span><span class="annot"><span class="annottext">Int -&gt; Int -&gt; Bool
forall a. Ord a =&gt; a -&gt; a -&gt; Bool
</span><a href="../../ghc-prim/src/GHC.Classes.html#%3C"><span class="hs-operator hs-var">&lt;</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><span class="hs-number">0</span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">String -&gt; IO (ForeignPtr b)
forall a. String -&gt; a
</span><a href="GHC.Err.html#errorWithoutStackTrace"><span class="hs-identifier hs-var">errorWithoutStackTrace</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;mallocForeignPtr: size must be &gt;= 0&quot;</span></span><span>
</span><span id="line-282"></span><span>          </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="GHC.Base.html#otherwise"><span class="hs-identifier hs-var">otherwise</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-283"></span><span>          </span><span id="local-6989586621679534724"><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534724"><span class="hs-identifier hs-var">r</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">Finalizers -&gt; IO (IORef Finalizers)
forall a. a -&gt; IO (IORef a)
</span><a href="GHC.IORef.html#newIORef"><span class="hs-identifier hs-var">newIORef</span></a></span><span> </span><span class="annot"><span class="annottext">Finalizers
</span><a href="GHC.ForeignPtr.html#NoFinalizers"><span class="hs-identifier hs-var">NoFinalizers</span></a></span><span>
</span><span id="line-284"></span><span>          </span><span class="annot"><span class="annottext">(State# RealWorld -&gt; (# State# RealWorld, ForeignPtr b #))
-&gt; IO (ForeignPtr b)
forall a. (State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
</span><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-var">IO</span></a></span><span> </span><span class="annot"><span class="annottext">((State# RealWorld -&gt; (# State# RealWorld, ForeignPtr b #))
 -&gt; IO (ForeignPtr b))
-&gt; (State# RealWorld -&gt; (# State# RealWorld, ForeignPtr b #))
-&gt; IO (ForeignPtr b)
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679534722"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534722"><span class="hs-identifier hs-var">s</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-285"></span><span>            </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">Int#
-&gt; Int#
-&gt; State# RealWorld
-&gt; (# State# RealWorld, MutableByteArray# RealWorld #)
forall d.
Int# -&gt; Int# -&gt; State# d -&gt; (# State# d, MutableByteArray# d #)
</span><a href="../../ghc-prim/src/GHC.Prim.html#newAlignedPinnedByteArray%23"><span class="hs-identifier hs-var">newAlignedPinnedByteArray#</span></a></span><span> </span><span class="annot"><span class="annottext">Int#
</span><a href="#local-6989586621679534727"><span class="hs-identifier hs-var">size</span></a></span><span> </span><span class="annot"><span class="annottext">Int#
</span><a href="#local-6989586621679534721"><span class="hs-identifier hs-var">align</span></a></span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534722"><span class="hs-identifier hs-var">s</span></a></span><span> </span><span class="hs-keyword">of</span><span> </span><span class="hs-special">{</span><span> </span><span class="hs-special">(#</span><span> </span><span id="local-6989586621679534720"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534720"><span class="hs-identifier hs-var">s'</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621679534719"><span class="annot"><span class="annottext">MutableByteArray# RealWorld
</span><a href="#local-6989586621679534719"><span class="hs-identifier hs-var">mbarr#</span></a></span></span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-286"></span><span>             </span><span class="hs-special">(#</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534720"><span class="hs-identifier hs-var">s'</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Addr# -&gt; ForeignPtrContents -&gt; ForeignPtr b
forall a. Addr# -&gt; ForeignPtrContents -&gt; ForeignPtr a
</span><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-var">ForeignPtr</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">ByteArray# -&gt; Addr#
</span><a href="../../ghc-prim/src/GHC.Prim.html#byteArrayContents%23"><span class="hs-identifier hs-var">byteArrayContents#</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">MutableByteArray# RealWorld -&gt; ByteArray#
forall (a :: TYPE 'UnliftedRep) (b :: TYPE 'UnliftedRep). a -&gt; b
</span><a href="Unsafe.Coerce.html#unsafeCoerceUnlifted"><span class="hs-identifier hs-var">unsafeCoerceUnlifted</span></a></span><span> </span><span class="annot"><span class="annottext">MutableByteArray# RealWorld
</span><a href="#local-6989586621679534719"><span class="hs-identifier hs-var">mbarr#</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-287"></span><span>                               </span><span class="hs-special">(</span><span class="annot"><span class="annottext">MutableByteArray# RealWorld
-&gt; IORef Finalizers -&gt; ForeignPtrContents
</span><a href="GHC.ForeignPtr.html#MallocPtr"><span class="hs-identifier hs-var">MallocPtr</span></a></span><span> </span><span class="annot"><span class="annottext">MutableByteArray# RealWorld
</span><a href="#local-6989586621679534719"><span class="hs-identifier hs-var">mbarr#</span></a></span><span> </span><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534724"><span class="hs-identifier hs-var">r</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-special">#)</span><span>
</span><span id="line-288"></span><span>            </span><span class="hs-special">}</span><span>
</span><span id="line-289"></span><span>            </span><span class="hs-keyword">where</span><span> </span><span class="hs-glyph">!</span><span class="hs-special">(</span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#I%23"><span class="hs-identifier hs-type">I#</span></a></span><span> </span><span id="local-6989586621679534727"><span class="annot"><span class="annottext">Int#
</span><a href="#local-6989586621679534727"><span class="hs-identifier hs-var">size</span></a></span></span><span class="hs-special">)</span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">b -&gt; Int
forall a. Storable a =&gt; a -&gt; Int
</span><a href="Foreign.Storable.html#sizeOf"><span class="hs-identifier hs-var">sizeOf</span></a></span><span> </span><span class="annot"><span class="annottext">b
</span><a href="#local-6989586621679534728"><span class="hs-identifier hs-var">a</span></a></span><span>
</span><span id="line-290"></span><span>                  </span><span class="hs-glyph">!</span><span class="hs-special">(</span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#I%23"><span class="hs-identifier hs-type">I#</span></a></span><span> </span><span id="local-6989586621679534721"><span class="annot"><span class="annottext">Int#
</span><a href="#local-6989586621679534721"><span class="hs-identifier hs-var">align</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">b -&gt; Int
forall a. Storable a =&gt; a -&gt; Int
</span><a href="Foreign.Storable.html#alignment"><span class="hs-identifier hs-var">alignment</span></a></span><span> </span><span class="annot"><span class="annottext">b
</span><a href="#local-6989586621679534728"><span class="hs-identifier hs-var">a</span></a></span><span>
</span><span id="line-291"></span><span>
</span><span id="line-292"></span><span class="hs-comment">-- | This function is similar to 'mallocForeignPtr', except that the</span><span>
</span><span id="line-293"></span><span class="hs-comment">-- size of the memory required is given explicitly as a number of bytes.</span><span>
</span><span id="line-294"></span><span id="local-6989586621679534910"><span class="annot"><a href="GHC.ForeignPtr.html#mallocForeignPtrBytes"><span class="hs-identifier hs-type">mallocForeignPtrBytes</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Int"><span class="hs-identifier hs-type">Int</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534910"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span></span><span>
</span><span id="line-295"></span><span id="mallocForeignPtrBytes"><span class="annot"><span class="annottext">mallocForeignPtrBytes :: forall a. Int -&gt; IO (ForeignPtr a)
</span><a href="GHC.ForeignPtr.html#mallocForeignPtrBytes"><span class="hs-identifier hs-var hs-var">mallocForeignPtrBytes</span></a></span></span><span> </span><span id="local-6989586621679534711"><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679534711"><span class="hs-identifier hs-var">size</span></a></span></span><span> </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679534711"><span class="hs-identifier hs-var">size</span></a></span><span> </span><span class="annot"><span class="annottext">Int -&gt; Int -&gt; Bool
forall a. Ord a =&gt; a -&gt; a -&gt; Bool
</span><a href="../../ghc-prim/src/GHC.Classes.html#%3C"><span class="hs-operator hs-var">&lt;</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><span class="hs-number">0</span></span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-296"></span><span>  </span><span class="annot"><span class="annottext">String -&gt; IO (ForeignPtr a)
forall a. String -&gt; a
</span><a href="GHC.Err.html#errorWithoutStackTrace"><span class="hs-identifier hs-var">errorWithoutStackTrace</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;mallocForeignPtrBytes: size must be &gt;= 0&quot;</span></span><span>
</span><span id="line-297"></span><span class="annot"><a href="GHC.ForeignPtr.html#mallocForeignPtrBytes"><span class="hs-identifier hs-var">mallocForeignPtrBytes</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#I%23"><span class="hs-identifier hs-type">I#</span></a></span><span> </span><span id="local-6989586621679534710"><span class="annot"><span class="annottext">Int#
</span><a href="#local-6989586621679534710"><span class="hs-identifier hs-var">size</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-298"></span><span>  </span><span id="local-6989586621679534709"><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534709"><span class="hs-identifier hs-var">r</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">Finalizers -&gt; IO (IORef Finalizers)
forall a. a -&gt; IO (IORef a)
</span><a href="GHC.IORef.html#newIORef"><span class="hs-identifier hs-var">newIORef</span></a></span><span> </span><span class="annot"><span class="annottext">Finalizers
</span><a href="GHC.ForeignPtr.html#NoFinalizers"><span class="hs-identifier hs-var">NoFinalizers</span></a></span><span>
</span><span id="line-299"></span><span>  </span><span class="annot"><span class="annottext">(State# RealWorld -&gt; (# State# RealWorld, ForeignPtr a #))
-&gt; IO (ForeignPtr a)
forall a. (State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
</span><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-var">IO</span></a></span><span> </span><span class="annot"><span class="annottext">((State# RealWorld -&gt; (# State# RealWorld, ForeignPtr a #))
 -&gt; IO (ForeignPtr a))
-&gt; (State# RealWorld -&gt; (# State# RealWorld, ForeignPtr a #))
-&gt; IO (ForeignPtr a)
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679534708"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534708"><span class="hs-identifier hs-var">s</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-300"></span><span>     </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">Int#
-&gt; State# RealWorld
-&gt; (# State# RealWorld, MutableByteArray# RealWorld #)
forall d. Int# -&gt; State# d -&gt; (# State# d, MutableByteArray# d #)
</span><a href="../../ghc-prim/src/GHC.Prim.html#newPinnedByteArray%23"><span class="hs-identifier hs-var">newPinnedByteArray#</span></a></span><span> </span><span class="annot"><span class="annottext">Int#
</span><a href="#local-6989586621679534710"><span class="hs-identifier hs-var">size</span></a></span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534708"><span class="hs-identifier hs-var">s</span></a></span><span>      </span><span class="hs-keyword">of</span><span> </span><span class="hs-special">{</span><span> </span><span class="hs-special">(#</span><span> </span><span id="local-6989586621679534707"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534707"><span class="hs-identifier hs-var">s'</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621679534706"><span class="annot"><span class="annottext">MutableByteArray# RealWorld
</span><a href="#local-6989586621679534706"><span class="hs-identifier hs-var">mbarr#</span></a></span></span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-301"></span><span>       </span><span class="hs-special">(#</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534707"><span class="hs-identifier hs-var">s'</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Addr# -&gt; ForeignPtrContents -&gt; ForeignPtr a
forall a. Addr# -&gt; ForeignPtrContents -&gt; ForeignPtr a
</span><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-var">ForeignPtr</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">ByteArray# -&gt; Addr#
</span><a href="../../ghc-prim/src/GHC.Prim.html#byteArrayContents%23"><span class="hs-identifier hs-var">byteArrayContents#</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">MutableByteArray# RealWorld -&gt; ByteArray#
forall (a :: TYPE 'UnliftedRep) (b :: TYPE 'UnliftedRep). a -&gt; b
</span><a href="Unsafe.Coerce.html#unsafeCoerceUnlifted"><span class="hs-identifier hs-var">unsafeCoerceUnlifted</span></a></span><span> </span><span class="annot"><span class="annottext">MutableByteArray# RealWorld
</span><a href="#local-6989586621679534706"><span class="hs-identifier hs-var">mbarr#</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-302"></span><span>                         </span><span class="hs-special">(</span><span class="annot"><span class="annottext">MutableByteArray# RealWorld
-&gt; IORef Finalizers -&gt; ForeignPtrContents
</span><a href="GHC.ForeignPtr.html#MallocPtr"><span class="hs-identifier hs-var">MallocPtr</span></a></span><span> </span><span class="annot"><span class="annottext">MutableByteArray# RealWorld
</span><a href="#local-6989586621679534706"><span class="hs-identifier hs-var">mbarr#</span></a></span><span> </span><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534709"><span class="hs-identifier hs-var">r</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-special">#)</span><span>
</span><span id="line-303"></span><span>     </span><span class="hs-special">}</span><span>
</span><span id="line-304"></span><span>
</span><span id="line-305"></span><span class="hs-comment">-- | This function is similar to 'mallocForeignPtrBytes', except that the</span><span>
</span><span id="line-306"></span><span class="hs-comment">-- size and alignment of the memory required is given explicitly as numbers of</span><span>
</span><span id="line-307"></span><span class="hs-comment">-- bytes.</span><span>
</span><span id="line-308"></span><span id="local-6989586621679534908"><span class="annot"><a href="GHC.ForeignPtr.html#mallocForeignPtrAlignedBytes"><span class="hs-identifier hs-type">mallocForeignPtrAlignedBytes</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Int"><span class="hs-identifier hs-type">Int</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Int"><span class="hs-identifier hs-type">Int</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534908"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span></span><span>
</span><span id="line-309"></span><span id="mallocForeignPtrAlignedBytes"><span class="annot"><span class="annottext">mallocForeignPtrAlignedBytes :: forall a. Int -&gt; Int -&gt; IO (ForeignPtr a)
</span><a href="GHC.ForeignPtr.html#mallocForeignPtrAlignedBytes"><span class="hs-identifier hs-var hs-var">mallocForeignPtrAlignedBytes</span></a></span></span><span> </span><span id="local-6989586621679534702"><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679534702"><span class="hs-identifier hs-var">size</span></a></span></span><span> </span><span id="local-6989586621679534701"><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679534701"><span class="hs-identifier hs-var">_align</span></a></span></span><span> </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679534702"><span class="hs-identifier hs-var">size</span></a></span><span> </span><span class="annot"><span class="annottext">Int -&gt; Int -&gt; Bool
forall a. Ord a =&gt; a -&gt; a -&gt; Bool
</span><a href="../../ghc-prim/src/GHC.Classes.html#%3C"><span class="hs-operator hs-var">&lt;</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><span class="hs-number">0</span></span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-310"></span><span>  </span><span class="annot"><span class="annottext">String -&gt; IO (ForeignPtr a)
forall a. String -&gt; a
</span><a href="GHC.Err.html#errorWithoutStackTrace"><span class="hs-identifier hs-var">errorWithoutStackTrace</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;mallocForeignPtrAlignedBytes: size must be &gt;= 0&quot;</span></span><span>
</span><span id="line-311"></span><span class="annot"><a href="GHC.ForeignPtr.html#mallocForeignPtrAlignedBytes"><span class="hs-identifier hs-var">mallocForeignPtrAlignedBytes</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#I%23"><span class="hs-identifier hs-type">I#</span></a></span><span> </span><span id="local-6989586621679534700"><span class="annot"><span class="annottext">Int#
</span><a href="#local-6989586621679534700"><span class="hs-identifier hs-var">size</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#I%23"><span class="hs-identifier hs-type">I#</span></a></span><span> </span><span id="local-6989586621679534699"><span class="annot"><span class="annottext">Int#
</span><a href="#local-6989586621679534699"><span class="hs-identifier hs-var">align</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-312"></span><span>  </span><span id="local-6989586621679534698"><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534698"><span class="hs-identifier hs-var">r</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">Finalizers -&gt; IO (IORef Finalizers)
forall a. a -&gt; IO (IORef a)
</span><a href="GHC.IORef.html#newIORef"><span class="hs-identifier hs-var">newIORef</span></a></span><span> </span><span class="annot"><span class="annottext">Finalizers
</span><a href="GHC.ForeignPtr.html#NoFinalizers"><span class="hs-identifier hs-var">NoFinalizers</span></a></span><span>
</span><span id="line-313"></span><span>  </span><span class="annot"><span class="annottext">(State# RealWorld -&gt; (# State# RealWorld, ForeignPtr a #))
-&gt; IO (ForeignPtr a)
forall a. (State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
</span><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-var">IO</span></a></span><span> </span><span class="annot"><span class="annottext">((State# RealWorld -&gt; (# State# RealWorld, ForeignPtr a #))
 -&gt; IO (ForeignPtr a))
-&gt; (State# RealWorld -&gt; (# State# RealWorld, ForeignPtr a #))
-&gt; IO (ForeignPtr a)
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679534697"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534697"><span class="hs-identifier hs-var">s</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-314"></span><span>     </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">Int#
-&gt; Int#
-&gt; State# RealWorld
-&gt; (# State# RealWorld, MutableByteArray# RealWorld #)
forall d.
Int# -&gt; Int# -&gt; State# d -&gt; (# State# d, MutableByteArray# d #)
</span><a href="../../ghc-prim/src/GHC.Prim.html#newAlignedPinnedByteArray%23"><span class="hs-identifier hs-var">newAlignedPinnedByteArray#</span></a></span><span> </span><span class="annot"><span class="annottext">Int#
</span><a href="#local-6989586621679534700"><span class="hs-identifier hs-var">size</span></a></span><span> </span><span class="annot"><span class="annottext">Int#
</span><a href="#local-6989586621679534699"><span class="hs-identifier hs-var">align</span></a></span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534697"><span class="hs-identifier hs-var">s</span></a></span><span> </span><span class="hs-keyword">of</span><span> </span><span class="hs-special">{</span><span> </span><span class="hs-special">(#</span><span> </span><span id="local-6989586621679534696"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534696"><span class="hs-identifier hs-var">s'</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621679534695"><span class="annot"><span class="annottext">MutableByteArray# RealWorld
</span><a href="#local-6989586621679534695"><span class="hs-identifier hs-var">mbarr#</span></a></span></span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-315"></span><span>       </span><span class="hs-special">(#</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534696"><span class="hs-identifier hs-var">s'</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Addr# -&gt; ForeignPtrContents -&gt; ForeignPtr a
forall a. Addr# -&gt; ForeignPtrContents -&gt; ForeignPtr a
</span><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-var">ForeignPtr</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">ByteArray# -&gt; Addr#
</span><a href="../../ghc-prim/src/GHC.Prim.html#byteArrayContents%23"><span class="hs-identifier hs-var">byteArrayContents#</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">MutableByteArray# RealWorld -&gt; ByteArray#
forall (a :: TYPE 'UnliftedRep) (b :: TYPE 'UnliftedRep). a -&gt; b
</span><a href="Unsafe.Coerce.html#unsafeCoerceUnlifted"><span class="hs-identifier hs-var">unsafeCoerceUnlifted</span></a></span><span> </span><span class="annot"><span class="annottext">MutableByteArray# RealWorld
</span><a href="#local-6989586621679534695"><span class="hs-identifier hs-var">mbarr#</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-316"></span><span>                         </span><span class="hs-special">(</span><span class="annot"><span class="annottext">MutableByteArray# RealWorld
-&gt; IORef Finalizers -&gt; ForeignPtrContents
</span><a href="GHC.ForeignPtr.html#MallocPtr"><span class="hs-identifier hs-var">MallocPtr</span></a></span><span> </span><span class="annot"><span class="annottext">MutableByteArray# RealWorld
</span><a href="#local-6989586621679534695"><span class="hs-identifier hs-var">mbarr#</span></a></span><span> </span><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534698"><span class="hs-identifier hs-var">r</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-special">#)</span><span>
</span><span id="line-317"></span><span>     </span><span class="hs-special">}</span><span>
</span><span id="line-318"></span><span>
</span><span id="line-319"></span><span class="hs-comment">-- | Allocate some memory and return a 'ForeignPtr' to it.  The memory</span><span>
</span><span id="line-320"></span><span class="hs-comment">-- will be released automatically when the 'ForeignPtr' is discarded.</span><span>
</span><span id="line-321"></span><span class="hs-comment">--</span><span>
</span><span id="line-322"></span><span class="hs-comment">-- GHC notes: 'mallocPlainForeignPtr' has a heavily optimised</span><span>
</span><span id="line-323"></span><span class="hs-comment">-- implementation in GHC.  It uses pinned memory in the garbage</span><span>
</span><span id="line-324"></span><span class="hs-comment">-- collected heap, as for mallocForeignPtr. Unlike mallocForeignPtr, a</span><span>
</span><span id="line-325"></span><span class="hs-comment">-- ForeignPtr created with mallocPlainForeignPtr carries no finalizers.</span><span>
</span><span id="line-326"></span><span class="hs-comment">-- It is not possible to add a finalizer to a ForeignPtr created with</span><span>
</span><span id="line-327"></span><span class="hs-comment">-- mallocPlainForeignPtr. This is useful for ForeignPtrs that will live</span><span>
</span><span id="line-328"></span><span class="hs-comment">-- only inside Haskell (such as those created for packed strings).</span><span>
</span><span id="line-329"></span><span class="hs-comment">-- Attempts to add a finalizer to a ForeignPtr created this way, or to</span><span>
</span><span id="line-330"></span><span class="hs-comment">-- finalize such a pointer, will throw an exception.</span><span>
</span><span id="line-331"></span><span class="hs-comment">--</span><span>
</span><span id="line-332"></span><span id="local-6989586621679534694"><span class="annot"><a href="GHC.ForeignPtr.html#mallocPlainForeignPtr"><span class="hs-identifier hs-type">mallocPlainForeignPtr</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="Foreign.Storable.html#Storable"><span class="hs-identifier hs-type">Storable</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534694"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534694"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span></span><span>
</span><span id="line-333"></span><span id="mallocPlainForeignPtr"><span class="annot"><span class="annottext">mallocPlainForeignPtr :: forall a. Storable a =&gt; IO (ForeignPtr a)
</span><a href="GHC.ForeignPtr.html#mallocPlainForeignPtr"><span class="hs-identifier hs-var hs-var">mallocPlainForeignPtr</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">a -&gt; IO (ForeignPtr a)
forall b. Storable b =&gt; b -&gt; IO (ForeignPtr b)
</span><a href="#local-6989586621679534689"><span class="hs-identifier hs-var">doMalloc</span></a></span><span> </span><span class="annot"><span class="annottext">a
forall a. HasCallStack =&gt; a
</span><a href="GHC.Err.html#undefined"><span class="hs-identifier hs-var">undefined</span></a></span><span>
</span><span id="line-334"></span><span>  </span><span class="hs-keyword">where</span><span> </span><span id="local-6989586621679534688"><span class="annot"><a href="#local-6989586621679534689"><span class="hs-identifier hs-type">doMalloc</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="Foreign.Storable.html#Storable"><span class="hs-identifier hs-type">Storable</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534688"><span class="hs-identifier hs-type">b</span></a></span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679534688"><span class="hs-identifier hs-type">b</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534688"><span class="hs-identifier hs-type">b</span></a></span><span class="hs-special">)</span></span><span>
</span><span id="line-335"></span><span>        </span><span id="local-6989586621679534689"><span class="annot"><span class="annottext">doMalloc :: forall b. Storable b =&gt; b -&gt; IO (ForeignPtr b)
</span><a href="#local-6989586621679534689"><span class="hs-identifier hs-var hs-var">doMalloc</span></a></span></span><span> </span><span id="local-6989586621679534684"><span class="annot"><span class="annottext">b
</span><a href="#local-6989586621679534684"><span class="hs-identifier hs-var">a</span></a></span></span><span>
</span><span id="line-336"></span><span>          </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Int# -&gt; Int
</span><a href="../../ghc-prim/src/GHC.Types.html#I%23"><span class="hs-identifier hs-var">I#</span></a></span><span> </span><span class="annot"><span class="annottext">Int#
</span><a href="#local-6989586621679534683"><span class="hs-identifier hs-var">size</span></a></span><span> </span><span class="annot"><span class="annottext">Int -&gt; Int -&gt; Bool
forall a. Ord a =&gt; a -&gt; a -&gt; Bool
</span><a href="../../ghc-prim/src/GHC.Classes.html#%3C"><span class="hs-operator hs-var">&lt;</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><span class="hs-number">0</span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">String -&gt; IO (ForeignPtr b)
forall a. String -&gt; a
</span><a href="GHC.Err.html#errorWithoutStackTrace"><span class="hs-identifier hs-var">errorWithoutStackTrace</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;mallocForeignPtr: size must be &gt;= 0&quot;</span></span><span>
</span><span id="line-337"></span><span>          </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="GHC.Base.html#otherwise"><span class="hs-identifier hs-var">otherwise</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(State# RealWorld -&gt; (# State# RealWorld, ForeignPtr b #))
-&gt; IO (ForeignPtr b)
forall a. (State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
</span><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-var">IO</span></a></span><span> </span><span class="annot"><span class="annottext">((State# RealWorld -&gt; (# State# RealWorld, ForeignPtr b #))
 -&gt; IO (ForeignPtr b))
-&gt; (State# RealWorld -&gt; (# State# RealWorld, ForeignPtr b #))
-&gt; IO (ForeignPtr b)
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679534682"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534682"><span class="hs-identifier hs-var">s</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-338"></span><span>            </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">Int#
-&gt; Int#
-&gt; State# RealWorld
-&gt; (# State# RealWorld, MutableByteArray# RealWorld #)
forall d.
Int# -&gt; Int# -&gt; State# d -&gt; (# State# d, MutableByteArray# d #)
</span><a href="../../ghc-prim/src/GHC.Prim.html#newAlignedPinnedByteArray%23"><span class="hs-identifier hs-var">newAlignedPinnedByteArray#</span></a></span><span> </span><span class="annot"><span class="annottext">Int#
</span><a href="#local-6989586621679534683"><span class="hs-identifier hs-var">size</span></a></span><span> </span><span class="annot"><span class="annottext">Int#
</span><a href="#local-6989586621679534681"><span class="hs-identifier hs-var">align</span></a></span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534682"><span class="hs-identifier hs-var">s</span></a></span><span> </span><span class="hs-keyword">of</span><span> </span><span class="hs-special">{</span><span> </span><span class="hs-special">(#</span><span> </span><span id="local-6989586621679534680"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534680"><span class="hs-identifier hs-var">s'</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621679534679"><span class="annot"><span class="annottext">MutableByteArray# RealWorld
</span><a href="#local-6989586621679534679"><span class="hs-identifier hs-var">mbarr#</span></a></span></span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-339"></span><span>             </span><span class="hs-special">(#</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534680"><span class="hs-identifier hs-var">s'</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Addr# -&gt; ForeignPtrContents -&gt; ForeignPtr b
forall a. Addr# -&gt; ForeignPtrContents -&gt; ForeignPtr a
</span><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-var">ForeignPtr</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">ByteArray# -&gt; Addr#
</span><a href="../../ghc-prim/src/GHC.Prim.html#byteArrayContents%23"><span class="hs-identifier hs-var">byteArrayContents#</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">MutableByteArray# RealWorld -&gt; ByteArray#
forall (a :: TYPE 'UnliftedRep) (b :: TYPE 'UnliftedRep). a -&gt; b
</span><a href="Unsafe.Coerce.html#unsafeCoerceUnlifted"><span class="hs-identifier hs-var">unsafeCoerceUnlifted</span></a></span><span> </span><span class="annot"><span class="annottext">MutableByteArray# RealWorld
</span><a href="#local-6989586621679534679"><span class="hs-identifier hs-var">mbarr#</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-340"></span><span>                               </span><span class="hs-special">(</span><span class="annot"><span class="annottext">MutableByteArray# RealWorld -&gt; ForeignPtrContents
</span><a href="GHC.ForeignPtr.html#PlainPtr"><span class="hs-identifier hs-var">PlainPtr</span></a></span><span> </span><span class="annot"><span class="annottext">MutableByteArray# RealWorld
</span><a href="#local-6989586621679534679"><span class="hs-identifier hs-var">mbarr#</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-special">#)</span><span>
</span><span id="line-341"></span><span>            </span><span class="hs-special">}</span><span>
</span><span id="line-342"></span><span>            </span><span class="hs-keyword">where</span><span> </span><span class="hs-glyph">!</span><span class="hs-special">(</span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#I%23"><span class="hs-identifier hs-type">I#</span></a></span><span> </span><span id="local-6989586621679534683"><span class="annot"><span class="annottext">Int#
</span><a href="#local-6989586621679534683"><span class="hs-identifier hs-var">size</span></a></span></span><span class="hs-special">)</span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">b -&gt; Int
forall a. Storable a =&gt; a -&gt; Int
</span><a href="Foreign.Storable.html#sizeOf"><span class="hs-identifier hs-var">sizeOf</span></a></span><span> </span><span class="annot"><span class="annottext">b
</span><a href="#local-6989586621679534684"><span class="hs-identifier hs-var">a</span></a></span><span>
</span><span id="line-343"></span><span>                  </span><span class="hs-glyph">!</span><span class="hs-special">(</span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#I%23"><span class="hs-identifier hs-type">I#</span></a></span><span> </span><span id="local-6989586621679534681"><span class="annot"><span class="annottext">Int#
</span><a href="#local-6989586621679534681"><span class="hs-identifier hs-var">align</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">b -&gt; Int
forall a. Storable a =&gt; a -&gt; Int
</span><a href="Foreign.Storable.html#alignment"><span class="hs-identifier hs-var">alignment</span></a></span><span> </span><span class="annot"><span class="annottext">b
</span><a href="#local-6989586621679534684"><span class="hs-identifier hs-var">a</span></a></span><span>
</span><span id="line-344"></span><span>
</span><span id="line-345"></span><span class="hs-comment">-- | This function is similar to 'mallocForeignPtrBytes', except that</span><span>
</span><span id="line-346"></span><span class="hs-comment">-- the internally an optimised ForeignPtr representation with no</span><span>
</span><span id="line-347"></span><span class="hs-comment">-- finalizer is used. Attempts to add a finalizer will cause an</span><span>
</span><span id="line-348"></span><span class="hs-comment">-- exception to be thrown.</span><span>
</span><span id="line-349"></span><span id="local-6989586621679534676"><span class="annot"><a href="GHC.ForeignPtr.html#mallocPlainForeignPtrBytes"><span class="hs-identifier hs-type">mallocPlainForeignPtrBytes</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Int"><span class="hs-identifier hs-type">Int</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534676"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span></span><span>
</span><span id="line-350"></span><span id="mallocPlainForeignPtrBytes"><span class="annot"><span class="annottext">mallocPlainForeignPtrBytes :: forall a. Int -&gt; IO (ForeignPtr a)
</span><a href="GHC.ForeignPtr.html#mallocPlainForeignPtrBytes"><span class="hs-identifier hs-var hs-var">mallocPlainForeignPtrBytes</span></a></span></span><span> </span><span id="local-6989586621679534673"><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679534673"><span class="hs-identifier hs-var">size</span></a></span></span><span> </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679534673"><span class="hs-identifier hs-var">size</span></a></span><span> </span><span class="annot"><span class="annottext">Int -&gt; Int -&gt; Bool
forall a. Ord a =&gt; a -&gt; a -&gt; Bool
</span><a href="../../ghc-prim/src/GHC.Classes.html#%3C"><span class="hs-operator hs-var">&lt;</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><span class="hs-number">0</span></span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-351"></span><span>  </span><span class="annot"><span class="annottext">String -&gt; IO (ForeignPtr a)
forall a. String -&gt; a
</span><a href="GHC.Err.html#errorWithoutStackTrace"><span class="hs-identifier hs-var">errorWithoutStackTrace</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;mallocPlainForeignPtrBytes: size must be &gt;= 0&quot;</span></span><span>
</span><span id="line-352"></span><span class="annot"><a href="GHC.ForeignPtr.html#mallocPlainForeignPtrBytes"><span class="hs-identifier hs-var">mallocPlainForeignPtrBytes</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#I%23"><span class="hs-identifier hs-type">I#</span></a></span><span> </span><span id="local-6989586621679534672"><span class="annot"><span class="annottext">Int#
</span><a href="#local-6989586621679534672"><span class="hs-identifier hs-var">size</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(State# RealWorld -&gt; (# State# RealWorld, ForeignPtr a #))
-&gt; IO (ForeignPtr a)
forall a. (State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
</span><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-var">IO</span></a></span><span> </span><span class="annot"><span class="annottext">((State# RealWorld -&gt; (# State# RealWorld, ForeignPtr a #))
 -&gt; IO (ForeignPtr a))
-&gt; (State# RealWorld -&gt; (# State# RealWorld, ForeignPtr a #))
-&gt; IO (ForeignPtr a)
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679534671"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534671"><span class="hs-identifier hs-var">s</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-353"></span><span>    </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">Int#
-&gt; State# RealWorld
-&gt; (# State# RealWorld, MutableByteArray# RealWorld #)
forall d. Int# -&gt; State# d -&gt; (# State# d, MutableByteArray# d #)
</span><a href="../../ghc-prim/src/GHC.Prim.html#newPinnedByteArray%23"><span class="hs-identifier hs-var">newPinnedByteArray#</span></a></span><span> </span><span class="annot"><span class="annottext">Int#
</span><a href="#local-6989586621679534672"><span class="hs-identifier hs-var">size</span></a></span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534671"><span class="hs-identifier hs-var">s</span></a></span><span>      </span><span class="hs-keyword">of</span><span> </span><span class="hs-special">{</span><span> </span><span class="hs-special">(#</span><span> </span><span id="local-6989586621679534670"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534670"><span class="hs-identifier hs-var">s'</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621679534669"><span class="annot"><span class="annottext">MutableByteArray# RealWorld
</span><a href="#local-6989586621679534669"><span class="hs-identifier hs-var">mbarr#</span></a></span></span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-354"></span><span>       </span><span class="hs-special">(#</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534670"><span class="hs-identifier hs-var">s'</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Addr# -&gt; ForeignPtrContents -&gt; ForeignPtr a
forall a. Addr# -&gt; ForeignPtrContents -&gt; ForeignPtr a
</span><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-var">ForeignPtr</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">ByteArray# -&gt; Addr#
</span><a href="../../ghc-prim/src/GHC.Prim.html#byteArrayContents%23"><span class="hs-identifier hs-var">byteArrayContents#</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">MutableByteArray# RealWorld -&gt; ByteArray#
forall (a :: TYPE 'UnliftedRep) (b :: TYPE 'UnliftedRep). a -&gt; b
</span><a href="Unsafe.Coerce.html#unsafeCoerceUnlifted"><span class="hs-identifier hs-var">unsafeCoerceUnlifted</span></a></span><span> </span><span class="annot"><span class="annottext">MutableByteArray# RealWorld
</span><a href="#local-6989586621679534669"><span class="hs-identifier hs-var">mbarr#</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-355"></span><span>                         </span><span class="hs-special">(</span><span class="annot"><span class="annottext">MutableByteArray# RealWorld -&gt; ForeignPtrContents
</span><a href="GHC.ForeignPtr.html#PlainPtr"><span class="hs-identifier hs-var">PlainPtr</span></a></span><span> </span><span class="annot"><span class="annottext">MutableByteArray# RealWorld
</span><a href="#local-6989586621679534669"><span class="hs-identifier hs-var">mbarr#</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-special">#)</span><span>
</span><span id="line-356"></span><span>     </span><span class="hs-special">}</span><span>
</span><span id="line-357"></span><span>
</span><span id="line-358"></span><span class="hs-comment">-- | This function is similar to 'mallocForeignPtrAlignedBytes', except that</span><span>
</span><span id="line-359"></span><span class="hs-comment">-- the internally an optimised ForeignPtr representation with no</span><span>
</span><span id="line-360"></span><span class="hs-comment">-- finalizer is used. Attempts to add a finalizer will cause an</span><span>
</span><span id="line-361"></span><span class="hs-comment">-- exception to be thrown.</span><span>
</span><span id="line-362"></span><span id="local-6989586621679534668"><span class="annot"><a href="GHC.ForeignPtr.html#mallocPlainForeignPtrAlignedBytes"><span class="hs-identifier hs-type">mallocPlainForeignPtrAlignedBytes</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Int"><span class="hs-identifier hs-type">Int</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Int"><span class="hs-identifier hs-type">Int</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534668"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span></span><span>
</span><span id="line-363"></span><span id="mallocPlainForeignPtrAlignedBytes"><span class="annot"><span class="annottext">mallocPlainForeignPtrAlignedBytes :: forall a. Int -&gt; Int -&gt; IO (ForeignPtr a)
</span><a href="GHC.ForeignPtr.html#mallocPlainForeignPtrAlignedBytes"><span class="hs-identifier hs-var hs-var">mallocPlainForeignPtrAlignedBytes</span></a></span></span><span> </span><span id="local-6989586621679534665"><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679534665"><span class="hs-identifier hs-var">size</span></a></span></span><span> </span><span id="local-6989586621679534664"><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679534664"><span class="hs-identifier hs-var">_align</span></a></span></span><span> </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679534665"><span class="hs-identifier hs-var">size</span></a></span><span> </span><span class="annot"><span class="annottext">Int -&gt; Int -&gt; Bool
forall a. Ord a =&gt; a -&gt; a -&gt; Bool
</span><a href="../../ghc-prim/src/GHC.Classes.html#%3C"><span class="hs-operator hs-var">&lt;</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><span class="hs-number">0</span></span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-364"></span><span>  </span><span class="annot"><span class="annottext">String -&gt; IO (ForeignPtr a)
forall a. String -&gt; a
</span><a href="GHC.Err.html#errorWithoutStackTrace"><span class="hs-identifier hs-var">errorWithoutStackTrace</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;mallocPlainForeignPtrAlignedBytes: size must be &gt;= 0&quot;</span></span><span>
</span><span id="line-365"></span><span class="annot"><a href="GHC.ForeignPtr.html#mallocPlainForeignPtrAlignedBytes"><span class="hs-identifier hs-var">mallocPlainForeignPtrAlignedBytes</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#I%23"><span class="hs-identifier hs-type">I#</span></a></span><span> </span><span id="local-6989586621679534663"><span class="annot"><span class="annottext">Int#
</span><a href="#local-6989586621679534663"><span class="hs-identifier hs-var">size</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#I%23"><span class="hs-identifier hs-type">I#</span></a></span><span> </span><span id="local-6989586621679534662"><span class="annot"><span class="annottext">Int#
</span><a href="#local-6989586621679534662"><span class="hs-identifier hs-var">align</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(State# RealWorld -&gt; (# State# RealWorld, ForeignPtr a #))
-&gt; IO (ForeignPtr a)
forall a. (State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
</span><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-var">IO</span></a></span><span> </span><span class="annot"><span class="annottext">((State# RealWorld -&gt; (# State# RealWorld, ForeignPtr a #))
 -&gt; IO (ForeignPtr a))
-&gt; (State# RealWorld -&gt; (# State# RealWorld, ForeignPtr a #))
-&gt; IO (ForeignPtr a)
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679534661"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534661"><span class="hs-identifier hs-var">s</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-366"></span><span>    </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">Int#
-&gt; Int#
-&gt; State# RealWorld
-&gt; (# State# RealWorld, MutableByteArray# RealWorld #)
forall d.
Int# -&gt; Int# -&gt; State# d -&gt; (# State# d, MutableByteArray# d #)
</span><a href="../../ghc-prim/src/GHC.Prim.html#newAlignedPinnedByteArray%23"><span class="hs-identifier hs-var">newAlignedPinnedByteArray#</span></a></span><span> </span><span class="annot"><span class="annottext">Int#
</span><a href="#local-6989586621679534663"><span class="hs-identifier hs-var">size</span></a></span><span> </span><span class="annot"><span class="annottext">Int#
</span><a href="#local-6989586621679534662"><span class="hs-identifier hs-var">align</span></a></span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534661"><span class="hs-identifier hs-var">s</span></a></span><span> </span><span class="hs-keyword">of</span><span> </span><span class="hs-special">{</span><span> </span><span class="hs-special">(#</span><span> </span><span id="local-6989586621679534660"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534660"><span class="hs-identifier hs-var">s'</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621679534659"><span class="annot"><span class="annottext">MutableByteArray# RealWorld
</span><a href="#local-6989586621679534659"><span class="hs-identifier hs-var">mbarr#</span></a></span></span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-367"></span><span>       </span><span class="hs-special">(#</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534660"><span class="hs-identifier hs-var">s'</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Addr# -&gt; ForeignPtrContents -&gt; ForeignPtr a
forall a. Addr# -&gt; ForeignPtrContents -&gt; ForeignPtr a
</span><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-var">ForeignPtr</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">ByteArray# -&gt; Addr#
</span><a href="../../ghc-prim/src/GHC.Prim.html#byteArrayContents%23"><span class="hs-identifier hs-var">byteArrayContents#</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">MutableByteArray# RealWorld -&gt; ByteArray#
forall (a :: TYPE 'UnliftedRep) (b :: TYPE 'UnliftedRep). a -&gt; b
</span><a href="Unsafe.Coerce.html#unsafeCoerceUnlifted"><span class="hs-identifier hs-var">unsafeCoerceUnlifted</span></a></span><span> </span><span class="annot"><span class="annottext">MutableByteArray# RealWorld
</span><a href="#local-6989586621679534659"><span class="hs-identifier hs-var">mbarr#</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-368"></span><span>                         </span><span class="hs-special">(</span><span class="annot"><span class="annottext">MutableByteArray# RealWorld -&gt; ForeignPtrContents
</span><a href="GHC.ForeignPtr.html#PlainPtr"><span class="hs-identifier hs-var">PlainPtr</span></a></span><span> </span><span class="annot"><span class="annottext">MutableByteArray# RealWorld
</span><a href="#local-6989586621679534659"><span class="hs-identifier hs-var">mbarr#</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-special">#)</span><span>
</span><span id="line-369"></span><span>     </span><span class="hs-special">}</span><span>
</span><span id="line-370"></span><span>
</span><span id="line-371"></span><span id="local-6989586621679534902"><span class="annot"><a href="GHC.ForeignPtr.html#addForeignPtrFinalizer"><span class="hs-identifier hs-type">addForeignPtrFinalizer</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.ForeignPtr.html#FinalizerPtr"><span class="hs-identifier hs-type">FinalizerPtr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534902"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534902"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span></span><span>
</span><span id="line-372"></span><span class="hs-comment">-- ^ This function adds a finalizer to the given foreign object.  The</span><span>
</span><span id="line-373"></span><span class="hs-comment">-- finalizer will run /before/ all other finalizers for the same</span><span>
</span><span id="line-374"></span><span class="hs-comment">-- object which have already been registered.</span><span>
</span><span id="line-375"></span><span id="addForeignPtrFinalizer"><span class="annot"><span class="annottext">addForeignPtrFinalizer :: forall a. FinalizerPtr a -&gt; ForeignPtr a -&gt; IO ()
</span><a href="GHC.ForeignPtr.html#addForeignPtrFinalizer"><span class="hs-identifier hs-var hs-var">addForeignPtrFinalizer</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Ptr.html#FunPtr"><span class="hs-identifier hs-type">FunPtr</span></a></span><span> </span><span id="local-6989586621679534657"><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534657"><span class="hs-identifier hs-var">fp</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span id="local-6989586621679534656"><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534656"><span class="hs-identifier hs-var">p</span></a></span></span><span> </span><span id="local-6989586621679534655"><span class="annot"><span class="annottext">ForeignPtrContents
</span><a href="#local-6989586621679534655"><span class="hs-identifier hs-var">c</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">ForeignPtrContents
</span><a href="#local-6989586621679534655"><span class="hs-identifier hs-var">c</span></a></span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-376"></span><span>  </span><span class="annot"><a href="GHC.ForeignPtr.html#PlainForeignPtr"><span class="hs-identifier hs-type">PlainForeignPtr</span></a></span><span> </span><span id="local-6989586621679534654"><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534654"><span class="hs-identifier hs-var">r</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">IORef Finalizers -&gt; Addr# -&gt; Int# -&gt; Addr# -&gt; Addr# -&gt; () -&gt; IO ()
forall value.
IORef Finalizers
-&gt; Addr# -&gt; Int# -&gt; Addr# -&gt; Addr# -&gt; value -&gt; IO ()
</span><a href="GHC.ForeignPtr.html#insertCFinalizer"><span class="hs-identifier hs-var">insertCFinalizer</span></a></span><span> </span><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534654"><span class="hs-identifier hs-var">r</span></a></span><span> </span><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534657"><span class="hs-identifier hs-var">fp</span></a></span><span> </span><span class="annot"><span class="annottext">Int#
</span><span class="hs-number">0#</span></span><span> </span><span class="annot"><span class="annottext">Addr#
</span><a href="../../ghc-prim/src/GHC.Prim.html#nullAddr%23"><span class="hs-identifier hs-var">nullAddr#</span></a></span><span> </span><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534656"><span class="hs-identifier hs-var">p</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-377"></span><span>  </span><span class="annot"><a href="GHC.ForeignPtr.html#MallocPtr"><span class="hs-identifier hs-type">MallocPtr</span></a></span><span>     </span><span class="annot"><span class="annottext">MutableByteArray# RealWorld
</span><span class="hs-identifier">_</span></span><span> </span><span id="local-6989586621679534652"><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534652"><span class="hs-identifier hs-var">r</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">IORef Finalizers
-&gt; Addr# -&gt; Int# -&gt; Addr# -&gt; Addr# -&gt; ForeignPtrContents -&gt; IO ()
forall value.
IORef Finalizers
-&gt; Addr# -&gt; Int# -&gt; Addr# -&gt; Addr# -&gt; value -&gt; IO ()
</span><a href="GHC.ForeignPtr.html#insertCFinalizer"><span class="hs-identifier hs-var">insertCFinalizer</span></a></span><span> </span><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534652"><span class="hs-identifier hs-var">r</span></a></span><span> </span><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534657"><span class="hs-identifier hs-var">fp</span></a></span><span> </span><span class="annot"><span class="annottext">Int#
</span><span class="hs-number">0#</span></span><span> </span><span class="annot"><span class="annottext">Addr#
</span><a href="../../ghc-prim/src/GHC.Prim.html#nullAddr%23"><span class="hs-identifier hs-var">nullAddr#</span></a></span><span> </span><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534656"><span class="hs-identifier hs-var">p</span></a></span><span> </span><span class="annot"><span class="annottext">ForeignPtrContents
</span><a href="#local-6989586621679534655"><span class="hs-identifier hs-var">c</span></a></span><span>
</span><span id="line-378"></span><span>  </span><span class="annot"><span class="annottext">ForeignPtrContents
</span><span class="hs-identifier">_</span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">String -&gt; IO ()
forall a. String -&gt; a
</span><a href="GHC.Err.html#errorWithoutStackTrace"><span class="hs-identifier hs-var">errorWithoutStackTrace</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;GHC.ForeignPtr: attempt to add a finalizer to a plain pointer or a final pointer&quot;</span></span><span>
</span><span id="line-379"></span><span>
</span><span id="line-380"></span><span class="hs-comment">-- Note [MallocPtr finalizers] (#10904)</span><span>
</span><span id="line-381"></span><span class="hs-comment">--</span><span>
</span><span id="line-382"></span><span class="hs-comment">-- When we have C finalizers for a MallocPtr, the memory is</span><span>
</span><span id="line-383"></span><span class="hs-comment">-- heap-resident and would normally be recovered by the GC before the</span><span>
</span><span id="line-384"></span><span class="hs-comment">-- finalizers run.  To prevent the memory from being reused too early,</span><span>
</span><span id="line-385"></span><span class="hs-comment">-- we attach the MallocPtr constructor to the &quot;value&quot; field of the</span><span>
</span><span id="line-386"></span><span class="hs-comment">-- weak pointer when we call mkWeak# in ensureCFinalizerWeak below.</span><span>
</span><span id="line-387"></span><span class="hs-comment">-- The GC will keep this field alive until the finalizers have run.</span><span>
</span><span id="line-388"></span><span>
</span><span id="line-389"></span><span id="local-6989586621679534897"><span id="local-6989586621679534898"><span class="annot"><a href="GHC.ForeignPtr.html#addForeignPtrFinalizerEnv"><span class="hs-identifier hs-type">addForeignPtrFinalizerEnv</span></a></span><span> </span><span class="hs-glyph">::</span><span>
</span><span id="line-390"></span><span>  </span><span class="annot"><a href="GHC.ForeignPtr.html#FinalizerEnvPtr"><span class="hs-identifier hs-type">FinalizerEnvPtr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534898"><span class="hs-identifier hs-type">env</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534897"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Ptr.html#Ptr"><span class="hs-identifier hs-type">Ptr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534898"><span class="hs-identifier hs-type">env</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534897"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span></span></span><span>
</span><span id="line-391"></span><span class="hs-comment">-- ^ Like 'addForeignPtrFinalizer' but the finalizer is passed an additional</span><span>
</span><span id="line-392"></span><span class="hs-comment">-- environment parameter.</span><span>
</span><span id="line-393"></span><span id="addForeignPtrFinalizerEnv"><span class="annot"><span class="annottext">addForeignPtrFinalizerEnv :: forall env a.
FinalizerEnvPtr env a -&gt; Ptr env -&gt; ForeignPtr a -&gt; IO ()
</span><a href="GHC.ForeignPtr.html#addForeignPtrFinalizerEnv"><span class="hs-identifier hs-var hs-var">addForeignPtrFinalizerEnv</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Ptr.html#FunPtr"><span class="hs-identifier hs-type">FunPtr</span></a></span><span> </span><span id="local-6989586621679534651"><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534651"><span class="hs-identifier hs-var">fp</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Ptr.html#Ptr"><span class="hs-identifier hs-type">Ptr</span></a></span><span> </span><span id="local-6989586621679534649"><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534649"><span class="hs-identifier hs-var">ep</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span id="local-6989586621679534648"><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534648"><span class="hs-identifier hs-var">p</span></a></span></span><span> </span><span id="local-6989586621679534647"><span class="annot"><span class="annottext">ForeignPtrContents
</span><a href="#local-6989586621679534647"><span class="hs-identifier hs-var">c</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">ForeignPtrContents
</span><a href="#local-6989586621679534647"><span class="hs-identifier hs-var">c</span></a></span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-394"></span><span>  </span><span class="annot"><a href="GHC.ForeignPtr.html#PlainForeignPtr"><span class="hs-identifier hs-type">PlainForeignPtr</span></a></span><span> </span><span id="local-6989586621679534646"><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534646"><span class="hs-identifier hs-var">r</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">IORef Finalizers -&gt; Addr# -&gt; Int# -&gt; Addr# -&gt; Addr# -&gt; () -&gt; IO ()
forall value.
IORef Finalizers
-&gt; Addr# -&gt; Int# -&gt; Addr# -&gt; Addr# -&gt; value -&gt; IO ()
</span><a href="GHC.ForeignPtr.html#insertCFinalizer"><span class="hs-identifier hs-var">insertCFinalizer</span></a></span><span> </span><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534646"><span class="hs-identifier hs-var">r</span></a></span><span> </span><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534651"><span class="hs-identifier hs-var">fp</span></a></span><span> </span><span class="annot"><span class="annottext">Int#
</span><span class="hs-number">1#</span></span><span> </span><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534649"><span class="hs-identifier hs-var">ep</span></a></span><span> </span><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534648"><span class="hs-identifier hs-var">p</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-395"></span><span>  </span><span class="annot"><a href="GHC.ForeignPtr.html#MallocPtr"><span class="hs-identifier hs-type">MallocPtr</span></a></span><span>     </span><span class="annot"><span class="annottext">MutableByteArray# RealWorld
</span><span class="hs-identifier">_</span></span><span> </span><span id="local-6989586621679534645"><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534645"><span class="hs-identifier hs-var">r</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">IORef Finalizers
-&gt; Addr# -&gt; Int# -&gt; Addr# -&gt; Addr# -&gt; ForeignPtrContents -&gt; IO ()
forall value.
IORef Finalizers
-&gt; Addr# -&gt; Int# -&gt; Addr# -&gt; Addr# -&gt; value -&gt; IO ()
</span><a href="GHC.ForeignPtr.html#insertCFinalizer"><span class="hs-identifier hs-var">insertCFinalizer</span></a></span><span> </span><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534645"><span class="hs-identifier hs-var">r</span></a></span><span> </span><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534651"><span class="hs-identifier hs-var">fp</span></a></span><span> </span><span class="annot"><span class="annottext">Int#
</span><span class="hs-number">1#</span></span><span> </span><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534649"><span class="hs-identifier hs-var">ep</span></a></span><span> </span><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534648"><span class="hs-identifier hs-var">p</span></a></span><span> </span><span class="annot"><span class="annottext">ForeignPtrContents
</span><a href="#local-6989586621679534647"><span class="hs-identifier hs-var">c</span></a></span><span>
</span><span id="line-396"></span><span>  </span><span class="annot"><span class="annottext">ForeignPtrContents
</span><span class="hs-identifier">_</span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">String -&gt; IO ()
forall a. String -&gt; a
</span><a href="GHC.Err.html#errorWithoutStackTrace"><span class="hs-identifier hs-var">errorWithoutStackTrace</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;GHC.ForeignPtr: attempt to add a finalizer to a plain pointer or a final pointer&quot;</span></span><span>
</span><span id="line-397"></span><span>
</span><span id="line-398"></span><span id="local-6989586621679534936"><span class="annot"><a href="GHC.ForeignPtr.html#addForeignPtrConcFinalizer"><span class="hs-identifier hs-type">addForeignPtrConcFinalizer</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534936"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span></span><span>
</span><span id="line-399"></span><span class="hs-comment">-- ^This function adds a finalizer to the given @ForeignPtr@.  The</span><span>
</span><span id="line-400"></span><span class="hs-comment">-- finalizer will run /before/ all other finalizers for the same</span><span>
</span><span id="line-401"></span><span class="hs-comment">-- object which have already been registered.</span><span>
</span><span id="line-402"></span><span class="hs-comment">--</span><span>
</span><span id="line-403"></span><span class="hs-comment">-- This is a variant of @addForeignPtrFinalizer@, where the finalizer</span><span>
</span><span id="line-404"></span><span class="hs-comment">-- is an arbitrary @IO@ action.  When it is invoked, the finalizer</span><span>
</span><span id="line-405"></span><span class="hs-comment">-- will run in a new thread.</span><span>
</span><span id="line-406"></span><span class="hs-comment">--</span><span>
</span><span id="line-407"></span><span class="hs-comment">-- NB. Be very careful with these finalizers.  One common trap is that</span><span>
</span><span id="line-408"></span><span class="hs-comment">-- if a finalizer references another finalized value, it does not</span><span>
</span><span id="line-409"></span><span class="hs-comment">-- prevent that value from being finalized.  In particular, 'System.IO.Handle's</span><span>
</span><span id="line-410"></span><span class="hs-comment">-- are finalized objects, so a finalizer should not refer to a</span><span>
</span><span id="line-411"></span><span class="hs-comment">-- 'System.IO.Handle' (including 'System.IO.stdout', 'System.IO.stdin', or</span><span>
</span><span id="line-412"></span><span class="hs-comment">-- 'System.IO.stderr').</span><span>
</span><span id="line-413"></span><span class="hs-comment">--</span><span>
</span><span id="line-414"></span><span id="addForeignPtrConcFinalizer"><span class="annot"><span class="annottext">addForeignPtrConcFinalizer :: forall a. ForeignPtr a -&gt; IO () -&gt; IO ()
</span><a href="GHC.ForeignPtr.html#addForeignPtrConcFinalizer"><span class="hs-identifier hs-var hs-var">addForeignPtrConcFinalizer</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span class="annot"><span class="annottext">Addr#
</span><span class="hs-identifier">_</span></span><span> </span><span id="local-6989586621679534644"><span class="annot"><span class="annottext">ForeignPtrContents
</span><a href="#local-6989586621679534644"><span class="hs-identifier hs-var">c</span></a></span></span><span class="hs-special">)</span><span> </span><span id="local-6989586621679534643"><span class="annot"><span class="annottext">IO ()
</span><a href="#local-6989586621679534643"><span class="hs-identifier hs-var">finalizer</span></a></span></span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-415"></span><span>  </span><span class="annot"><span class="annottext">ForeignPtrContents -&gt; IO () -&gt; IO ()
</span><a href="GHC.ForeignPtr.html#addForeignPtrConcFinalizer_"><span class="hs-identifier hs-var">addForeignPtrConcFinalizer_</span></a></span><span> </span><span class="annot"><span class="annottext">ForeignPtrContents
</span><a href="#local-6989586621679534644"><span class="hs-identifier hs-var">c</span></a></span><span> </span><span class="annot"><span class="annottext">IO ()
</span><a href="#local-6989586621679534643"><span class="hs-identifier hs-var">finalizer</span></a></span><span>
</span><span id="line-416"></span><span>
</span><span id="line-417"></span><span class="annot"><a href="GHC.ForeignPtr.html#addForeignPtrConcFinalizer_"><span class="hs-identifier hs-type">addForeignPtrConcFinalizer_</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtrContents"><span class="hs-identifier hs-type">ForeignPtrContents</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-418"></span><span id="addForeignPtrConcFinalizer_"><span class="annot"><span class="annottext">addForeignPtrConcFinalizer_ :: ForeignPtrContents -&gt; IO () -&gt; IO ()
</span><a href="GHC.ForeignPtr.html#addForeignPtrConcFinalizer_"><span class="hs-identifier hs-var hs-var">addForeignPtrConcFinalizer_</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.ForeignPtr.html#PlainForeignPtr"><span class="hs-identifier hs-type">PlainForeignPtr</span></a></span><span> </span><span id="local-6989586621679534641"><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534641"><span class="hs-identifier hs-var">r</span></a></span></span><span class="hs-special">)</span><span> </span><span id="local-6989586621679534640"><span class="annot"><span class="annottext">IO ()
</span><a href="#local-6989586621679534640"><span class="hs-identifier hs-var">finalizer</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-419"></span><span>  </span><span id="local-6989586621679534639"><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621679534639"><span class="hs-identifier hs-var">noFinalizers</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">IORef Finalizers -&gt; IO () -&gt; IO Bool
</span><a href="GHC.ForeignPtr.html#insertHaskellFinalizer"><span class="hs-identifier hs-var">insertHaskellFinalizer</span></a></span><span> </span><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534641"><span class="hs-identifier hs-var">r</span></a></span><span> </span><span class="annot"><span class="annottext">IO ()
</span><a href="#local-6989586621679534640"><span class="hs-identifier hs-var">finalizer</span></a></span><span>
</span><span id="line-420"></span><span>  </span><span class="hs-keyword">if</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621679534639"><span class="hs-identifier hs-var">noFinalizers</span></a></span><span>
</span><span id="line-421"></span><span>     </span><span class="hs-keyword">then</span><span> </span><span class="annot"><span class="annottext">(State# RealWorld -&gt; (# State# RealWorld, () #)) -&gt; IO ()
forall a. (State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
</span><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-var">IO</span></a></span><span> </span><span class="annot"><span class="annottext">((State# RealWorld -&gt; (# State# RealWorld, () #)) -&gt; IO ())
-&gt; (State# RealWorld -&gt; (# State# RealWorld, () #)) -&gt; IO ()
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679534637"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534637"><span class="hs-identifier hs-var">s</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-422"></span><span>              </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534641"><span class="hs-identifier hs-var">r</span></a></span><span> </span><span class="hs-keyword">of</span><span> </span><span class="hs-special">{</span><span> </span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.STRef.html#STRef"><span class="hs-identifier hs-type">STRef</span></a></span><span> </span><span id="local-6989586621679534634"><span class="annot"><span class="annottext">MutVar# RealWorld Finalizers
</span><a href="#local-6989586621679534634"><span class="hs-identifier hs-var">r#</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-423"></span><span>              </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">MutVar# RealWorld Finalizers
-&gt; ()
-&gt; (State# RealWorld -&gt; (# State# RealWorld, () #))
-&gt; State# RealWorld
-&gt; (# State# RealWorld, Weak# () #)
</span><a href="../../ghc-prim/src/GHC.Prim.html#mkWeak%23"><span class="hs-identifier hs-var">mkWeak#</span></a></span><span> </span><span class="annot"><span class="annottext">MutVar# RealWorld Finalizers
</span><a href="#local-6989586621679534634"><span class="hs-identifier hs-var">r#</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">IO () -&gt; State# RealWorld -&gt; (# State# RealWorld, () #)
forall a. IO a -&gt; State# RealWorld -&gt; (# State# RealWorld, a #)
</span><a href="GHC.Base.html#unIO"><span class="hs-identifier hs-var">unIO</span></a></span><span> </span><span class="annot"><span class="annottext">(IO () -&gt; State# RealWorld -&gt; (# State# RealWorld, () #))
-&gt; IO () -&gt; State# RealWorld -&gt; (# State# RealWorld, () #)
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="annot"><span class="annottext">IORef Finalizers -&gt; IO ()
</span><a href="GHC.ForeignPtr.html#foreignPtrFinalizer"><span class="hs-identifier hs-var">foreignPtrFinalizer</span></a></span><span> </span><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534641"><span class="hs-identifier hs-var">r</span></a></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534637"><span class="hs-identifier hs-var">s</span></a></span><span> </span><span class="hs-keyword">of</span><span> </span><span class="hs-special">{</span><span>
</span><span id="line-424"></span><span>                </span><span class="hs-special">(#</span><span> </span><span id="local-6989586621679534631"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534631"><span class="hs-identifier hs-var">s1</span></a></span></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Weak# ()
</span><span class="hs-identifier">_</span></span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(#</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534631"><span class="hs-identifier hs-var">s1</span></a></span><span class="hs-special">,</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-special">}</span><span class="hs-special">}</span><span>
</span><span id="line-425"></span><span>     </span><span class="hs-keyword">else</span><span> </span><span class="annot"><span class="annottext">() -&gt; IO ()
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-426"></span><span class="annot"><a href="GHC.ForeignPtr.html#addForeignPtrConcFinalizer_"><span class="hs-identifier hs-var">addForeignPtrConcFinalizer_</span></a></span><span> </span><span id="local-6989586621679534630"><span class="annot"><span class="annottext">f :: ForeignPtrContents
</span><a href="#local-6989586621679534630"><span class="hs-identifier hs-var">f</span></a></span></span><span class="hs-glyph">@</span><span class="hs-special">(</span><span class="annot"><a href="GHC.ForeignPtr.html#MallocPtr"><span class="hs-identifier hs-type">MallocPtr</span></a></span><span> </span><span id="local-6989586621679534629"><span class="annot"><span class="annottext">MutableByteArray# RealWorld
</span><a href="#local-6989586621679534629"><span class="hs-identifier hs-var">fo</span></a></span></span><span> </span><span id="local-6989586621679534628"><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534628"><span class="hs-identifier hs-var">r</span></a></span></span><span class="hs-special">)</span><span> </span><span id="local-6989586621679534627"><span class="annot"><span class="annottext">IO ()
</span><a href="#local-6989586621679534627"><span class="hs-identifier hs-var">finalizer</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-427"></span><span>  </span><span id="local-6989586621679534626"><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621679534626"><span class="hs-identifier hs-var">noFinalizers</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">IORef Finalizers -&gt; IO () -&gt; IO Bool
</span><a href="GHC.ForeignPtr.html#insertHaskellFinalizer"><span class="hs-identifier hs-var">insertHaskellFinalizer</span></a></span><span> </span><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534628"><span class="hs-identifier hs-var">r</span></a></span><span> </span><span class="annot"><span class="annottext">IO ()
</span><a href="#local-6989586621679534627"><span class="hs-identifier hs-var">finalizer</span></a></span><span>
</span><span id="line-428"></span><span>  </span><span class="hs-keyword">if</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621679534626"><span class="hs-identifier hs-var">noFinalizers</span></a></span><span>
</span><span id="line-429"></span><span>     </span><span class="hs-keyword">then</span><span>  </span><span class="annot"><span class="annottext">(State# RealWorld -&gt; (# State# RealWorld, () #)) -&gt; IO ()
forall a. (State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
</span><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-var">IO</span></a></span><span> </span><span class="annot"><span class="annottext">((State# RealWorld -&gt; (# State# RealWorld, () #)) -&gt; IO ())
-&gt; (State# RealWorld -&gt; (# State# RealWorld, () #)) -&gt; IO ()
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679534625"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534625"><span class="hs-identifier hs-var">s</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-430"></span><span>               </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">MutableByteArray# RealWorld
-&gt; ()
-&gt; (State# RealWorld -&gt; (# State# RealWorld, () #))
-&gt; State# RealWorld
-&gt; (# State# RealWorld, Weak# () #)
</span><a href="../../ghc-prim/src/GHC.Prim.html#mkWeak%23"><span class="hs-identifier hs-var">mkWeak#</span></a></span><span> </span><span class="annot"><span class="annottext">MutableByteArray# RealWorld
</span><a href="#local-6989586621679534629"><span class="hs-identifier hs-var">fo</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">State# RealWorld -&gt; (# State# RealWorld, () #)
</span><a href="#local-6989586621679534624"><span class="hs-identifier hs-var">finalizer'</span></a></span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534625"><span class="hs-identifier hs-var">s</span></a></span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-431"></span><span>                  </span><span class="hs-special">(#</span><span> </span><span id="local-6989586621679534623"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534623"><span class="hs-identifier hs-var">s1</span></a></span></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Weak# ()
</span><span class="hs-identifier">_</span></span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(#</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534623"><span class="hs-identifier hs-var">s1</span></a></span><span class="hs-special">,</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span> </span><span class="hs-special">#)</span><span>
</span><span id="line-432"></span><span>     </span><span class="hs-keyword">else</span><span> </span><span class="annot"><span class="annottext">() -&gt; IO ()
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-433"></span><span>  </span><span class="hs-keyword">where</span><span>
</span><span id="line-434"></span><span>    </span><span class="annot"><a href="#local-6989586621679534624"><span class="hs-identifier hs-type">finalizer'</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Prim.html#State%23"><span class="hs-identifier hs-type">State#</span></a></span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Prim.html#RealWorld"><span class="hs-identifier hs-type">RealWorld</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(#</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Prim.html#State%23"><span class="hs-identifier hs-type">State#</span></a></span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Prim.html#RealWorld"><span class="hs-identifier hs-type">RealWorld</span></a></span><span class="hs-special">,</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span> </span><span class="hs-special">#)</span><span>
</span><span id="line-435"></span><span>    </span><span id="local-6989586621679534624"><span class="annot"><span class="annottext">finalizer' :: State# RealWorld -&gt; (# State# RealWorld, () #)
</span><a href="#local-6989586621679534624"><span class="hs-identifier hs-var hs-var">finalizer'</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">IO () -&gt; State# RealWorld -&gt; (# State# RealWorld, () #)
forall a. IO a -&gt; State# RealWorld -&gt; (# State# RealWorld, a #)
</span><a href="GHC.Base.html#unIO"><span class="hs-identifier hs-var">unIO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">IORef Finalizers -&gt; IO ()
</span><a href="GHC.ForeignPtr.html#foreignPtrFinalizer"><span class="hs-identifier hs-var">foreignPtrFinalizer</span></a></span><span> </span><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534628"><span class="hs-identifier hs-var">r</span></a></span><span> </span><span class="annot"><span class="annottext">IO () -&gt; IO () -&gt; IO ()
forall (m :: * -&gt; *) a b. Monad m =&gt; m a -&gt; m b -&gt; m b
</span><a href="GHC.Base.html#%3E%3E"><span class="hs-operator hs-var">&gt;&gt;</span></a></span><span> </span><span class="annot"><span class="annottext">ForeignPtrContents -&gt; IO ()
</span><a href="GHC.ForeignPtr.html#touch"><span class="hs-identifier hs-var">touch</span></a></span><span> </span><span class="annot"><span class="annottext">ForeignPtrContents
</span><a href="#local-6989586621679534630"><span class="hs-identifier hs-var">f</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-436"></span><span>
</span><span id="line-437"></span><span class="annot"><a href="GHC.ForeignPtr.html#addForeignPtrConcFinalizer_"><span class="hs-identifier hs-var">addForeignPtrConcFinalizer_</span></a></span><span> </span><span class="annot"><span class="annottext">ForeignPtrContents
</span><span class="hs-identifier">_</span></span><span> </span><span class="annot"><span class="annottext">IO ()
</span><span class="hs-identifier">_</span></span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-438"></span><span>  </span><span class="annot"><span class="annottext">String -&gt; IO ()
forall a. String -&gt; a
</span><a href="GHC.Err.html#errorWithoutStackTrace"><span class="hs-identifier hs-var">errorWithoutStackTrace</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;GHC.ForeignPtr: attempt to add a finalizer to plain pointer or a final pointer&quot;</span></span><span>
</span><span id="line-439"></span><span>
</span><span id="line-440"></span><span class="annot"><a href="GHC.ForeignPtr.html#insertHaskellFinalizer"><span class="hs-identifier hs-type">insertHaskellFinalizer</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span class="annot"><a href="GHC.ForeignPtr.html#Finalizers"><span class="hs-identifier hs-type">Finalizers</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Bool"><span class="hs-identifier hs-type">Bool</span></a></span><span>
</span><span id="line-441"></span><span id="insertHaskellFinalizer"><span class="annot"><span class="annottext">insertHaskellFinalizer :: IORef Finalizers -&gt; IO () -&gt; IO Bool
</span><a href="GHC.ForeignPtr.html#insertHaskellFinalizer"><span class="hs-identifier hs-var hs-var">insertHaskellFinalizer</span></a></span></span><span> </span><span id="local-6989586621679534621"><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534621"><span class="hs-identifier hs-var">r</span></a></span></span><span> </span><span id="local-6989586621679534620"><span class="annot"><span class="annottext">IO ()
</span><a href="#local-6989586621679534620"><span class="hs-identifier hs-var">f</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-442"></span><span>  </span><span class="hs-glyph">!</span><span id="local-6989586621679534619"><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621679534619"><span class="hs-identifier hs-var">wasEmpty</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">IORef Finalizers -&gt; (Finalizers -&gt; (Finalizers, Bool)) -&gt; IO Bool
forall a b. IORef a -&gt; (a -&gt; (a, b)) -&gt; IO b
</span><a href="GHC.IORef.html#atomicModifyIORefP"><span class="hs-identifier hs-var">atomicModifyIORefP</span></a></span><span> </span><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534621"><span class="hs-identifier hs-var">r</span></a></span><span> </span><span class="annot"><span class="annottext">((Finalizers -&gt; (Finalizers, Bool)) -&gt; IO Bool)
-&gt; (Finalizers -&gt; (Finalizers, Bool)) -&gt; IO Bool
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679534617"><span class="annot"><span class="annottext">Finalizers
</span><a href="#local-6989586621679534617"><span class="hs-identifier hs-var">finalizers</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">Finalizers
</span><a href="#local-6989586621679534617"><span class="hs-identifier hs-var">finalizers</span></a></span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-443"></span><span>      </span><span class="annot"><span class="annottext">Finalizers
</span><a href="GHC.ForeignPtr.html#NoFinalizers"><span class="hs-identifier hs-var">NoFinalizers</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">[IO ()] -&gt; Finalizers
</span><a href="GHC.ForeignPtr.html#HaskellFinalizers"><span class="hs-identifier hs-var">HaskellFinalizers</span></a></span><span> </span><span class="hs-special">[</span><span class="annot"><span class="annottext">IO ()
</span><a href="#local-6989586621679534620"><span class="hs-identifier hs-var">f</span></a></span><span class="hs-special">]</span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="../../ghc-prim/src/GHC.Types.html#True"><span class="hs-identifier hs-var">True</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-444"></span><span>      </span><span class="annot"><a href="GHC.ForeignPtr.html#HaskellFinalizers"><span class="hs-identifier hs-type">HaskellFinalizers</span></a></span><span> </span><span id="local-6989586621679534616"><span class="annot"><span class="annottext">[IO ()]
</span><a href="#local-6989586621679534616"><span class="hs-identifier hs-var">fs</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">[IO ()] -&gt; Finalizers
</span><a href="GHC.ForeignPtr.html#HaskellFinalizers"><span class="hs-identifier hs-var">HaskellFinalizers</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">IO ()
</span><a href="#local-6989586621679534620"><span class="hs-identifier hs-var">f</span></a></span><span class="annot"><span class="annottext">IO () -&gt; [IO ()] -&gt; [IO ()]
forall a. a -&gt; [a] -&gt; [a]
</span><a href="../../ghc-prim/src/GHC.Types.html#%3A"><span class="hs-glyph hs-var">:</span></a></span><span class="annot"><span class="annottext">[IO ()]
</span><a href="#local-6989586621679534616"><span class="hs-identifier hs-var">fs</span></a></span><span class="hs-special">)</span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="../../ghc-prim/src/GHC.Types.html#False"><span class="hs-identifier hs-var">False</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-445"></span><span>      </span><span class="annot"><span class="annottext">Finalizers
</span><span class="hs-identifier">_</span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">(Finalizers, Bool)
forall a. a
</span><a href="GHC.ForeignPtr.html#noMixingError"><span class="hs-identifier hs-var">noMixingError</span></a></span><span>
</span><span id="line-446"></span><span>  </span><span class="annot"><span class="annottext">Bool -&gt; IO Bool
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621679534619"><span class="hs-identifier hs-var">wasEmpty</span></a></span><span>
</span><span id="line-447"></span><span>
</span><span id="line-448"></span><span class="hs-comment">-- | A box around Weak#, private to this module.</span><span>
</span><span id="line-449"></span><span class="hs-keyword">data</span><span> </span><span id="MyWeak"><span class="annot"><a href="GHC.ForeignPtr.html#MyWeak"><span class="hs-identifier hs-var">MyWeak</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span id="MyWeak"><span class="annot"><a href="GHC.ForeignPtr.html#MyWeak"><span class="hs-identifier hs-var">MyWeak</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="../../ghc-prim/src/GHC.Prim.html#Weak%23"><span class="hs-identifier hs-type">Weak#</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-450"></span><span>
</span><span id="line-451"></span><span id="local-6989586621679534899"><span class="annot"><a href="GHC.ForeignPtr.html#insertCFinalizer"><span class="hs-identifier hs-type">insertCFinalizer</span></a></span><span> </span><span class="hs-glyph">::</span><span>
</span><span id="line-452"></span><span>  </span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span class="annot"><a href="GHC.ForeignPtr.html#Finalizers"><span class="hs-identifier hs-type">Finalizers</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Prim.html#Addr%23"><span class="hs-identifier hs-type">Addr#</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Prim.html#Int%23"><span class="hs-identifier hs-type">Int#</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Prim.html#Addr%23"><span class="hs-identifier hs-type">Addr#</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Prim.html#Addr%23"><span class="hs-identifier hs-type">Addr#</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679534899"><span class="hs-identifier hs-type">value</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span></span><span>
</span><span id="line-453"></span><span id="insertCFinalizer"><span class="annot"><span class="annottext">insertCFinalizer :: forall value.
IORef Finalizers
-&gt; Addr# -&gt; Int# -&gt; Addr# -&gt; Addr# -&gt; value -&gt; IO ()
</span><a href="GHC.ForeignPtr.html#insertCFinalizer"><span class="hs-identifier hs-var hs-var">insertCFinalizer</span></a></span></span><span> </span><span id="local-6989586621679534612"><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534612"><span class="hs-identifier hs-var">r</span></a></span></span><span> </span><span id="local-6989586621679534611"><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534611"><span class="hs-identifier hs-var">fp</span></a></span></span><span> </span><span id="local-6989586621679534610"><span class="annot"><span class="annottext">Int#
</span><a href="#local-6989586621679534610"><span class="hs-identifier hs-var">flag</span></a></span></span><span> </span><span id="local-6989586621679534609"><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534609"><span class="hs-identifier hs-var">ep</span></a></span></span><span> </span><span id="local-6989586621679534608"><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534608"><span class="hs-identifier hs-var">p</span></a></span></span><span> </span><span id="local-6989586621679534607"><span class="annot"><span class="annottext">value
</span><a href="#local-6989586621679534607"><span class="hs-identifier hs-var">val</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-454"></span><span>  </span><span class="annot"><a href="GHC.ForeignPtr.html#MyWeak"><span class="hs-identifier hs-type">MyWeak</span></a></span><span> </span><span id="local-6989586621679534606"><span class="annot"><span class="annottext">Weak# ()
</span><a href="#local-6989586621679534606"><span class="hs-identifier hs-var">w</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">IORef Finalizers -&gt; value -&gt; IO MyWeak
forall value. IORef Finalizers -&gt; value -&gt; IO MyWeak
</span><a href="GHC.ForeignPtr.html#ensureCFinalizerWeak"><span class="hs-identifier hs-var">ensureCFinalizerWeak</span></a></span><span> </span><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534612"><span class="hs-identifier hs-var">r</span></a></span><span> </span><span class="annot"><span class="annottext">value
</span><a href="#local-6989586621679534607"><span class="hs-identifier hs-var">val</span></a></span><span>
</span><span id="line-455"></span><span>  </span><span class="annot"><span class="annottext">(State# RealWorld -&gt; (# State# RealWorld, () #)) -&gt; IO ()
forall a. (State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
</span><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-var">IO</span></a></span><span> </span><span class="annot"><span class="annottext">((State# RealWorld -&gt; (# State# RealWorld, () #)) -&gt; IO ())
-&gt; (State# RealWorld -&gt; (# State# RealWorld, () #)) -&gt; IO ()
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679534604"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534604"><span class="hs-identifier hs-var">s</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">Addr#
-&gt; Addr#
-&gt; Int#
-&gt; Addr#
-&gt; Weak# ()
-&gt; State# RealWorld
-&gt; (# State# RealWorld, Int# #)
forall b.
Addr#
-&gt; Addr#
-&gt; Int#
-&gt; Addr#
-&gt; Weak# b
-&gt; State# RealWorld
-&gt; (# State# RealWorld, Int# #)
</span><a href="../../ghc-prim/src/GHC.Prim.html#addCFinalizerToWeak%23"><span class="hs-identifier hs-var">addCFinalizerToWeak#</span></a></span><span> </span><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534611"><span class="hs-identifier hs-var">fp</span></a></span><span> </span><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534608"><span class="hs-identifier hs-var">p</span></a></span><span> </span><span class="annot"><span class="annottext">Int#
</span><a href="#local-6989586621679534610"><span class="hs-identifier hs-var">flag</span></a></span><span> </span><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534609"><span class="hs-identifier hs-var">ep</span></a></span><span> </span><span class="annot"><span class="annottext">Weak# ()
</span><a href="#local-6989586621679534606"><span class="hs-identifier hs-var">w</span></a></span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534604"><span class="hs-identifier hs-var">s</span></a></span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-456"></span><span>      </span><span class="hs-special">(#</span><span> </span><span id="local-6989586621679534603"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534603"><span class="hs-identifier hs-var">s1</span></a></span></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Int#
</span><span class="hs-number">1#</span></span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(#</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534603"><span class="hs-identifier hs-var">s1</span></a></span><span class="hs-special">,</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span> </span><span class="hs-special">#)</span><span>
</span><span id="line-457"></span><span>
</span><span id="line-458"></span><span>      </span><span class="hs-comment">-- Failed to add the finalizer because some other thread</span><span>
</span><span id="line-459"></span><span>      </span><span class="hs-comment">-- has finalized w by calling foreignPtrFinalizer. We retry now.</span><span>
</span><span id="line-460"></span><span>      </span><span class="hs-comment">-- This won't be an infinite loop because that thread must have</span><span>
</span><span id="line-461"></span><span>      </span><span class="hs-comment">-- replaced the content of r before calling finalizeWeak#.</span><span>
</span><span id="line-462"></span><span>      </span><span class="hs-special">(#</span><span> </span><span id="local-6989586621679534602"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534602"><span class="hs-identifier hs-var">s1</span></a></span></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Int#
</span><span class="hs-identifier">_</span></span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">IO () -&gt; State# RealWorld -&gt; (# State# RealWorld, () #)
forall a. IO a -&gt; State# RealWorld -&gt; (# State# RealWorld, a #)
</span><a href="GHC.Base.html#unIO"><span class="hs-identifier hs-var">unIO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">IORef Finalizers
-&gt; Addr# -&gt; Int# -&gt; Addr# -&gt; Addr# -&gt; value -&gt; IO ()
forall value.
IORef Finalizers
-&gt; Addr# -&gt; Int# -&gt; Addr# -&gt; Addr# -&gt; value -&gt; IO ()
</span><a href="GHC.ForeignPtr.html#insertCFinalizer"><span class="hs-identifier hs-var">insertCFinalizer</span></a></span><span> </span><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534612"><span class="hs-identifier hs-var">r</span></a></span><span> </span><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534611"><span class="hs-identifier hs-var">fp</span></a></span><span> </span><span class="annot"><span class="annottext">Int#
</span><a href="#local-6989586621679534610"><span class="hs-identifier hs-var">flag</span></a></span><span> </span><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534609"><span class="hs-identifier hs-var">ep</span></a></span><span> </span><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534608"><span class="hs-identifier hs-var">p</span></a></span><span> </span><span class="annot"><span class="annottext">value
</span><a href="#local-6989586621679534607"><span class="hs-identifier hs-var">val</span></a></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534602"><span class="hs-identifier hs-var">s1</span></a></span><span>
</span><span id="line-463"></span><span>
</span><span id="line-464"></span><span class="hs-comment">-- Read the weak reference from an IORef Finalizers, creating it if necessary.</span><span>
</span><span id="line-465"></span><span class="hs-comment">-- Throws an exception if HaskellFinalizers is encountered.</span><span>
</span><span id="line-466"></span><span id="local-6989586621679534879"><span class="annot"><a href="GHC.ForeignPtr.html#ensureCFinalizerWeak"><span class="hs-identifier hs-type">ensureCFinalizerWeak</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span class="annot"><a href="GHC.ForeignPtr.html#Finalizers"><span class="hs-identifier hs-type">Finalizers</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="#local-6989586621679534879"><span class="hs-identifier hs-type">value</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="GHC.ForeignPtr.html#MyWeak"><span class="hs-identifier hs-type">MyWeak</span></a></span></span><span>
</span><span id="line-467"></span><span id="ensureCFinalizerWeak"><span class="annot"><span class="annottext">ensureCFinalizerWeak :: forall value. IORef Finalizers -&gt; value -&gt; IO MyWeak
</span><a href="GHC.ForeignPtr.html#ensureCFinalizerWeak"><span class="hs-identifier hs-var hs-var">ensureCFinalizerWeak</span></a></span></span><span> </span><span id="local-6989586621679534599"><span class="annot"><span class="annottext">ref :: IORef Finalizers
</span><a href="#local-6989586621679534599"><span class="hs-identifier hs-var">ref</span></a></span></span><span class="hs-glyph">@</span><span class="hs-special">(</span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.STRef.html#STRef"><span class="hs-identifier hs-type">STRef</span></a></span><span> </span><span id="local-6989586621679534598"><span class="annot"><span class="annottext">MutVar# RealWorld Finalizers
</span><a href="#local-6989586621679534598"><span class="hs-identifier hs-var">r#</span></a></span></span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span id="local-6989586621679534597"><span class="annot"><span class="annottext">value
</span><a href="#local-6989586621679534597"><span class="hs-identifier hs-var">value</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-468"></span><span>  </span><span id="local-6989586621679534596"><span class="annot"><span class="annottext">Finalizers
</span><a href="#local-6989586621679534596"><span class="hs-identifier hs-var">fin</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">IORef Finalizers -&gt; IO Finalizers
forall a. IORef a -&gt; IO a
</span><a href="GHC.IORef.html#readIORef"><span class="hs-identifier hs-var">readIORef</span></a></span><span> </span><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534599"><span class="hs-identifier hs-var">ref</span></a></span><span>
</span><span id="line-469"></span><span>  </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">Finalizers
</span><a href="#local-6989586621679534596"><span class="hs-identifier hs-var">fin</span></a></span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-470"></span><span>      </span><span class="annot"><a href="GHC.ForeignPtr.html#CFinalizers"><span class="hs-identifier hs-type">CFinalizers</span></a></span><span> </span><span id="local-6989586621679534594"><span class="annot"><span class="annottext">Weak# ()
</span><a href="#local-6989586621679534594"><span class="hs-identifier hs-var">weak</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">MyWeak -&gt; IO MyWeak
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Weak# () -&gt; MyWeak
</span><a href="GHC.ForeignPtr.html#MyWeak"><span class="hs-identifier hs-var">MyWeak</span></a></span><span> </span><span class="annot"><span class="annottext">Weak# ()
</span><a href="#local-6989586621679534594"><span class="hs-identifier hs-var">weak</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-471"></span><span>      </span><span class="annot"><a href="GHC.ForeignPtr.html#HaskellFinalizers"><span class="hs-identifier hs-type">HaskellFinalizers</span></a></span><span class="hs-special">{</span><span class="hs-special">}</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">IO MyWeak
forall a. a
</span><a href="GHC.ForeignPtr.html#noMixingError"><span class="hs-identifier hs-var">noMixingError</span></a></span><span>
</span><span id="line-472"></span><span>      </span><span class="annot"><span class="annottext">Finalizers
</span><a href="GHC.ForeignPtr.html#NoFinalizers"><span class="hs-identifier hs-var">NoFinalizers</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">(State# RealWorld -&gt; (# State# RealWorld, MyWeak #)) -&gt; IO MyWeak
forall a. (State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
</span><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-var">IO</span></a></span><span> </span><span class="annot"><span class="annottext">((State# RealWorld -&gt; (# State# RealWorld, MyWeak #)) -&gt; IO MyWeak)
-&gt; (State# RealWorld -&gt; (# State# RealWorld, MyWeak #))
-&gt; IO MyWeak
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679534593"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534593"><span class="hs-identifier hs-var">s</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-473"></span><span>          </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">MutVar# RealWorld Finalizers
-&gt; () -&gt; State# RealWorld -&gt; (# State# RealWorld, Weak# () #)
</span><a href="../../ghc-prim/src/GHC.Prim.html#mkWeakNoFinalizer%23"><span class="hs-identifier hs-var">mkWeakNoFinalizer#</span></a></span><span> </span><span class="annot"><span class="annottext">MutVar# RealWorld Finalizers
</span><a href="#local-6989586621679534598"><span class="hs-identifier hs-var">r#</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">value -&gt; ()
forall a b. a -&gt; b
</span><a href="Unsafe.Coerce.html#unsafeCoerce"><span class="hs-identifier hs-var">unsafeCoerce</span></a></span><span> </span><span class="annot"><span class="annottext">value
</span><a href="#local-6989586621679534597"><span class="hs-identifier hs-var">value</span></a></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534593"><span class="hs-identifier hs-var">s</span></a></span><span> </span><span class="hs-keyword">of</span><span> </span><span class="hs-special">{</span><span> </span><span class="hs-special">(#</span><span> </span><span id="local-6989586621679534592"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534592"><span class="hs-identifier hs-var">s1</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621679534591"><span class="annot"><span class="annottext">Weak# ()
</span><a href="#local-6989586621679534591"><span class="hs-identifier hs-var">w</span></a></span></span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-474"></span><span>             </span><span class="hs-comment">-- See Note [MallocPtr finalizers] (#10904)</span><span>
</span><span id="line-475"></span><span>          </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">MutVar# RealWorld Finalizers
-&gt; (Finalizers -&gt; (Finalizers, (MyWeak, Bool)))
-&gt; State# RealWorld
-&gt; (# State# RealWorld, Finalizers, (Finalizers, (MyWeak, Bool)) #)
forall d a c.
MutVar# d a -&gt; (a -&gt; c) -&gt; State# d -&gt; (# State# d, a, c #)
</span><a href="../../ghc-prim/src/GHC.Prim.html#atomicModifyMutVar2%23"><span class="hs-identifier hs-var">atomicModifyMutVar2#</span></a></span><span> </span><span class="annot"><span class="annottext">MutVar# RealWorld Finalizers
</span><a href="#local-6989586621679534598"><span class="hs-identifier hs-var">r#</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Weak# () -&gt; Finalizers -&gt; (Finalizers, (MyWeak, Bool))
</span><a href="#local-6989586621679534590"><span class="hs-identifier hs-var">update</span></a></span><span> </span><span class="annot"><span class="annottext">Weak# ()
</span><a href="#local-6989586621679534591"><span class="hs-identifier hs-var">w</span></a></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534592"><span class="hs-identifier hs-var">s1</span></a></span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-476"></span><span>              </span><span class="hs-special">{</span><span> </span><span class="hs-special">(#</span><span> </span><span id="local-6989586621679534589"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534589"><span class="hs-identifier hs-var">s2</span></a></span></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Finalizers
</span><span class="hs-identifier">_</span></span><span class="hs-special">,</span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Finalizers
</span><span class="hs-identifier">_</span></span><span class="hs-special">,</span><span> </span><span class="hs-special">(</span><span id="local-6989586621679534588"><span class="annot"><span class="annottext">MyWeak
</span><a href="#local-6989586621679534588"><span class="hs-identifier hs-var">weak</span></a></span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621679534587"><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621679534587"><span class="hs-identifier hs-var">needKill</span></a></span></span><span> </span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-477"></span><span>          </span><span class="hs-keyword">if</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621679534587"><span class="hs-identifier hs-var">needKill</span></a></span><span>
</span><span id="line-478"></span><span>            </span><span class="hs-keyword">then</span><span> </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">Weak# ()
-&gt; State# RealWorld
-&gt; (# State# RealWorld, Int#,
      State# RealWorld -&gt; (# State# RealWorld, Any #) #)
forall a b.
Weak# a
-&gt; State# RealWorld
-&gt; (# State# RealWorld, Int#,
      State# RealWorld -&gt; (# State# RealWorld, b #) #)
</span><a href="../../ghc-prim/src/GHC.Prim.html#finalizeWeak%23"><span class="hs-identifier hs-var">finalizeWeak#</span></a></span><span> </span><span class="annot"><span class="annottext">Weak# ()
</span><a href="#local-6989586621679534591"><span class="hs-identifier hs-var">w</span></a></span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534589"><span class="hs-identifier hs-var">s2</span></a></span><span> </span><span class="hs-keyword">of</span><span> </span><span class="hs-special">{</span><span> </span><span class="hs-special">(#</span><span> </span><span id="local-6989586621679534586"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534586"><span class="hs-identifier hs-var">s3</span></a></span></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Int#
</span><span class="hs-identifier">_</span></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">State# RealWorld -&gt; (# State# RealWorld, Any #)
</span><span class="hs-identifier">_</span></span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-479"></span><span>              </span><span class="hs-special">(#</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534586"><span class="hs-identifier hs-var">s3</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">MyWeak
</span><a href="#local-6989586621679534588"><span class="hs-identifier hs-var">weak</span></a></span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-special">}</span><span>
</span><span id="line-480"></span><span>            </span><span class="hs-keyword">else</span><span> </span><span class="hs-special">(#</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534589"><span class="hs-identifier hs-var">s2</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">MyWeak
</span><a href="#local-6989586621679534588"><span class="hs-identifier hs-var">weak</span></a></span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-special">}</span><span class="hs-special">}</span><span>
</span><span id="line-481"></span><span>  </span><span class="hs-keyword">where</span><span>
</span><span id="line-482"></span><span>      </span><span id="local-6989586621679534590"><span class="annot"><span class="annottext">update :: Weak# () -&gt; Finalizers -&gt; (Finalizers, (MyWeak, Bool))
</span><a href="#local-6989586621679534590"><span class="hs-identifier hs-var hs-var">update</span></a></span></span><span> </span><span class="annot"><span class="annottext">Weak# ()
</span><span class="hs-identifier">_</span></span><span> </span><span id="local-6989586621679534585"><span class="annot"><span class="annottext">fin :: Finalizers
</span><a href="#local-6989586621679534585"><span class="hs-identifier hs-var">fin</span></a></span></span><span class="hs-glyph">@</span><span class="hs-special">(</span><span class="annot"><a href="GHC.ForeignPtr.html#CFinalizers"><span class="hs-identifier hs-type">CFinalizers</span></a></span><span> </span><span id="local-6989586621679534584"><span class="annot"><span class="annottext">Weak# ()
</span><a href="#local-6989586621679534584"><span class="hs-identifier hs-var">w</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Finalizers
</span><a href="#local-6989586621679534585"><span class="hs-identifier hs-var">fin</span></a></span><span class="hs-special">,</span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Weak# () -&gt; MyWeak
</span><a href="GHC.ForeignPtr.html#MyWeak"><span class="hs-identifier hs-var">MyWeak</span></a></span><span> </span><span class="annot"><span class="annottext">Weak# ()
</span><a href="#local-6989586621679534584"><span class="hs-identifier hs-var">w</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="../../ghc-prim/src/GHC.Types.html#True"><span class="hs-identifier hs-var">True</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-483"></span><span>      </span><span class="annot"><a href="#local-6989586621679534590"><span class="hs-identifier hs-var">update</span></a></span><span> </span><span id="local-6989586621679534583"><span class="annot"><span class="annottext">Weak# ()
</span><a href="#local-6989586621679534583"><span class="hs-identifier hs-var">w</span></a></span></span><span> </span><span class="annot"><span class="annottext">Finalizers
</span><a href="GHC.ForeignPtr.html#NoFinalizers"><span class="hs-identifier hs-var">NoFinalizers</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Weak# () -&gt; Finalizers
</span><a href="GHC.ForeignPtr.html#CFinalizers"><span class="hs-identifier hs-var">CFinalizers</span></a></span><span> </span><span class="annot"><span class="annottext">Weak# ()
</span><a href="#local-6989586621679534583"><span class="hs-identifier hs-var">w</span></a></span><span class="hs-special">,</span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Weak# () -&gt; MyWeak
</span><a href="GHC.ForeignPtr.html#MyWeak"><span class="hs-identifier hs-var">MyWeak</span></a></span><span> </span><span class="annot"><span class="annottext">Weak# ()
</span><a href="#local-6989586621679534583"><span class="hs-identifier hs-var">w</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="../../ghc-prim/src/GHC.Types.html#False"><span class="hs-identifier hs-var">False</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-484"></span><span>      </span><span class="annot"><a href="#local-6989586621679534590"><span class="hs-identifier hs-var">update</span></a></span><span> </span><span class="annot"><span class="annottext">Weak# ()
</span><span class="hs-identifier">_</span></span><span> </span><span class="annot"><span class="annottext">Finalizers
</span><span class="hs-identifier">_</span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(Finalizers, (MyWeak, Bool))
forall a. a
</span><a href="GHC.ForeignPtr.html#noMixingError"><span class="hs-identifier hs-var">noMixingError</span></a></span><span>
</span><span id="line-485"></span><span>
</span><span id="line-486"></span><span id="local-6989586621679534882"><span class="annot"><a href="GHC.ForeignPtr.html#noMixingError"><span class="hs-identifier hs-type">noMixingError</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="#local-6989586621679534882"><span class="hs-identifier hs-type">a</span></a></span></span><span>
</span><span id="line-487"></span><span id="noMixingError"><span class="annot"><span class="annottext">noMixingError :: forall a. a
</span><a href="GHC.ForeignPtr.html#noMixingError"><span class="hs-identifier hs-var hs-var">noMixingError</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">String -&gt; a
forall a. String -&gt; a
</span><a href="GHC.Err.html#errorWithoutStackTrace"><span class="hs-identifier hs-var">errorWithoutStackTrace</span></a></span><span> </span><span class="annot"><span class="annottext">(String -&gt; a) -&gt; String -&gt; a
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span>
</span><span id="line-488"></span><span>   </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;GHC.ForeignPtr: attempt to mix Haskell and C finalizers &quot;</span></span><span> </span><span class="annot"><span class="annottext">String -&gt; ShowS
forall a. [a] -&gt; [a] -&gt; [a]
</span><a href="GHC.Base.html#%2B%2B"><span class="hs-operator hs-var">++</span></a></span><span>
</span><span id="line-489"></span><span>   </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;in the same ForeignPtr&quot;</span></span><span>
</span><span id="line-490"></span><span>
</span><span id="line-491"></span><span class="hs-comment">-- Swap out the finalizers with NoFinalizers and then run them.</span><span>
</span><span id="line-492"></span><span class="annot"><a href="GHC.ForeignPtr.html#foreignPtrFinalizer"><span class="hs-identifier hs-type">foreignPtrFinalizer</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span class="annot"><a href="GHC.ForeignPtr.html#Finalizers"><span class="hs-identifier hs-type">Finalizers</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-493"></span><span id="foreignPtrFinalizer"><span class="annot"><span class="annottext">foreignPtrFinalizer :: IORef Finalizers -&gt; IO ()
</span><a href="GHC.ForeignPtr.html#foreignPtrFinalizer"><span class="hs-identifier hs-var hs-var">foreignPtrFinalizer</span></a></span></span><span> </span><span id="local-6989586621679534582"><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534582"><span class="hs-identifier hs-var">r</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-494"></span><span>  </span><span id="local-6989586621679534581"><span class="annot"><span class="annottext">Finalizers
</span><a href="#local-6989586621679534581"><span class="hs-identifier hs-var">fs</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">IORef Finalizers -&gt; Finalizers -&gt; IO Finalizers
forall a. IORef a -&gt; a -&gt; IO a
</span><a href="GHC.IORef.html#atomicSwapIORef"><span class="hs-identifier hs-var">atomicSwapIORef</span></a></span><span> </span><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534582"><span class="hs-identifier hs-var">r</span></a></span><span> </span><span class="annot"><span class="annottext">Finalizers
</span><a href="GHC.ForeignPtr.html#NoFinalizers"><span class="hs-identifier hs-var">NoFinalizers</span></a></span><span>
</span><span id="line-495"></span><span>             </span><span class="hs-comment">-- atomic, see #7170</span><span>
</span><span id="line-496"></span><span>  </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">Finalizers
</span><a href="#local-6989586621679534581"><span class="hs-identifier hs-var">fs</span></a></span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-497"></span><span>    </span><span class="annot"><span class="annottext">Finalizers
</span><a href="GHC.ForeignPtr.html#NoFinalizers"><span class="hs-identifier hs-var">NoFinalizers</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">() -&gt; IO ()
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-498"></span><span>    </span><span class="annot"><a href="GHC.ForeignPtr.html#CFinalizers"><span class="hs-identifier hs-type">CFinalizers</span></a></span><span> </span><span id="local-6989586621679534579"><span class="annot"><span class="annottext">Weak# ()
</span><a href="#local-6989586621679534579"><span class="hs-identifier hs-var">w</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">(State# RealWorld -&gt; (# State# RealWorld, () #)) -&gt; IO ()
forall a. (State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
</span><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-var">IO</span></a></span><span> </span><span class="annot"><span class="annottext">((State# RealWorld -&gt; (# State# RealWorld, () #)) -&gt; IO ())
-&gt; (State# RealWorld -&gt; (# State# RealWorld, () #)) -&gt; IO ()
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679534578"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534578"><span class="hs-identifier hs-var">s</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">Weak# ()
-&gt; State# RealWorld
-&gt; (# State# RealWorld, Int#,
      State# RealWorld -&gt; (# State# RealWorld, () #) #)
forall a b.
Weak# a
-&gt; State# RealWorld
-&gt; (# State# RealWorld, Int#,
      State# RealWorld -&gt; (# State# RealWorld, b #) #)
</span><a href="../../ghc-prim/src/GHC.Prim.html#finalizeWeak%23"><span class="hs-identifier hs-var">finalizeWeak#</span></a></span><span> </span><span class="annot"><span class="annottext">Weak# ()
</span><a href="#local-6989586621679534579"><span class="hs-identifier hs-var">w</span></a></span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534578"><span class="hs-identifier hs-var">s</span></a></span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-499"></span><span>        </span><span class="hs-special">(#</span><span> </span><span id="local-6989586621679534577"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534577"><span class="hs-identifier hs-var">s1</span></a></span></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Int#
</span><span class="hs-number">1#</span></span><span class="hs-special">,</span><span> </span><span id="local-6989586621679534576"><span class="annot"><span class="annottext">State# RealWorld -&gt; (# State# RealWorld, () #)
</span><a href="#local-6989586621679534576"><span class="hs-identifier hs-var">f</span></a></span></span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">State# RealWorld -&gt; (# State# RealWorld, () #)
</span><a href="#local-6989586621679534576"><span class="hs-identifier hs-var">f</span></a></span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534577"><span class="hs-identifier hs-var">s1</span></a></span><span>
</span><span id="line-500"></span><span>        </span><span class="hs-special">(#</span><span> </span><span id="local-6989586621679534575"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534575"><span class="hs-identifier hs-var">s1</span></a></span></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">Int#
</span><span class="hs-identifier">_</span></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">State# RealWorld -&gt; (# State# RealWorld, () #)
</span><span class="hs-identifier">_</span></span><span> </span><span class="hs-special">#)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(#</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534575"><span class="hs-identifier hs-var">s1</span></a></span><span class="hs-special">,</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span> </span><span class="hs-special">#)</span><span>
</span><span id="line-501"></span><span>    </span><span class="annot"><a href="GHC.ForeignPtr.html#HaskellFinalizers"><span class="hs-identifier hs-type">HaskellFinalizers</span></a></span><span> </span><span id="local-6989586621679534574"><span class="annot"><span class="annottext">[IO ()]
</span><a href="#local-6989586621679534574"><span class="hs-identifier hs-var">actions</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">[IO ()] -&gt; IO ()
forall (t :: * -&gt; *) (m :: * -&gt; *) a.
(Foldable t, Monad m) =&gt;
t (m a) -&gt; m ()
</span><a href="Data.Foldable.html#sequence_"><span class="hs-identifier hs-var">sequence_</span></a></span><span> </span><span class="annot"><span class="annottext">[IO ()]
</span><a href="#local-6989586621679534574"><span class="hs-identifier hs-var">actions</span></a></span><span>
</span><span id="line-502"></span><span>
</span><span id="line-503"></span><span id="local-6989586621679534937"><span class="annot"><a href="GHC.ForeignPtr.html#newForeignPtr_"><span class="hs-identifier hs-type">newForeignPtr_</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.Ptr.html#Ptr"><span class="hs-identifier hs-type">Ptr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534937"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534937"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span></span><span>
</span><span id="line-504"></span><span class="hs-comment">-- ^Turns a plain memory reference into a foreign pointer that may be</span><span>
</span><span id="line-505"></span><span class="hs-comment">-- associated with finalizers by using 'addForeignPtrFinalizer'.</span><span>
</span><span id="line-506"></span><span id="newForeignPtr_"><span class="annot"><span class="annottext">newForeignPtr_ :: forall a. Ptr a -&gt; IO (ForeignPtr a)
</span><a href="GHC.ForeignPtr.html#newForeignPtr_"><span class="hs-identifier hs-var hs-var">newForeignPtr_</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Ptr.html#Ptr"><span class="hs-identifier hs-type">Ptr</span></a></span><span> </span><span id="local-6989586621679534571"><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534571"><span class="hs-identifier hs-var">obj</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span>  </span><span class="hs-keyword">do</span><span>
</span><span id="line-507"></span><span>  </span><span id="local-6989586621679534570"><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534570"><span class="hs-identifier hs-var">r</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">Finalizers -&gt; IO (IORef Finalizers)
forall a. a -&gt; IO (IORef a)
</span><a href="GHC.IORef.html#newIORef"><span class="hs-identifier hs-var">newIORef</span></a></span><span> </span><span class="annot"><span class="annottext">Finalizers
</span><a href="GHC.ForeignPtr.html#NoFinalizers"><span class="hs-identifier hs-var">NoFinalizers</span></a></span><span>
</span><span id="line-508"></span><span>  </span><span class="annot"><span class="annottext">ForeignPtr a -&gt; IO (ForeignPtr a)
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Addr# -&gt; ForeignPtrContents -&gt; ForeignPtr a
forall a. Addr# -&gt; ForeignPtrContents -&gt; ForeignPtr a
</span><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-var">ForeignPtr</span></a></span><span> </span><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534571"><span class="hs-identifier hs-var">obj</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">IORef Finalizers -&gt; ForeignPtrContents
</span><a href="GHC.ForeignPtr.html#PlainForeignPtr"><span class="hs-identifier hs-var">PlainForeignPtr</span></a></span><span> </span><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534570"><span class="hs-identifier hs-var">r</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-509"></span><span>
</span><span id="line-510"></span><span id="local-6989586621679534866"><span id="local-6989586621679534867"><span class="annot"><a href="GHC.ForeignPtr.html#withForeignPtr"><span class="hs-identifier hs-type">withForeignPtr</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534867"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Ptr.html#Ptr"><span class="hs-identifier hs-type">Ptr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534867"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534866"><span class="hs-identifier hs-type">b</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534866"><span class="hs-identifier hs-type">b</span></a></span></span></span><span>
</span><span id="line-511"></span><span class="hs-comment">-- ^This is a way to look at the pointer living inside a</span><span>
</span><span id="line-512"></span><span class="hs-comment">-- foreign object.  This function takes a function which is</span><span>
</span><span id="line-513"></span><span class="hs-comment">-- applied to that pointer. The resulting 'IO' action is then</span><span>
</span><span id="line-514"></span><span class="hs-comment">-- executed. The foreign object is kept alive at least during</span><span>
</span><span id="line-515"></span><span class="hs-comment">-- the whole action, even if it is not used directly</span><span>
</span><span id="line-516"></span><span class="hs-comment">-- inside. Note that it is not safe to return the pointer from</span><span>
</span><span id="line-517"></span><span class="hs-comment">-- the action and use it after the action completes. All uses</span><span>
</span><span id="line-518"></span><span class="hs-comment">-- of the pointer should be inside the</span><span>
</span><span id="line-519"></span><span class="hs-comment">-- 'withForeignPtr' bracket.  The reason for</span><span>
</span><span id="line-520"></span><span class="hs-comment">-- this unsafeness is the same as for</span><span>
</span><span id="line-521"></span><span class="hs-comment">-- 'unsafeForeignPtrToPtr' below: the finalizer</span><span>
</span><span id="line-522"></span><span class="hs-comment">-- may run earlier than expected, because the compiler can only</span><span>
</span><span id="line-523"></span><span class="hs-comment">-- track usage of the 'ForeignPtr' object, not</span><span>
</span><span id="line-524"></span><span class="hs-comment">-- a 'Ptr' object made from it.</span><span>
</span><span id="line-525"></span><span class="hs-comment">--</span><span>
</span><span id="line-526"></span><span class="hs-comment">-- This function is normally used for marshalling data to</span><span>
</span><span id="line-527"></span><span class="hs-comment">-- or from the object pointed to by the</span><span>
</span><span id="line-528"></span><span class="hs-comment">-- 'ForeignPtr', using the operations from the</span><span>
</span><span id="line-529"></span><span class="hs-comment">-- 'Storable' class.</span><span>
</span><span id="line-530"></span><span id="withForeignPtr"><span class="annot"><span class="annottext">withForeignPtr :: forall a b. ForeignPtr a -&gt; (Ptr a -&gt; IO b) -&gt; IO b
</span><a href="GHC.ForeignPtr.html#withForeignPtr"><span class="hs-identifier hs-var hs-var">withForeignPtr</span></a></span></span><span> </span><span id="local-6989586621679534569"><span class="annot"><span class="annottext">fo :: ForeignPtr a
</span><a href="#local-6989586621679534569"><span class="hs-identifier hs-var">fo</span></a></span></span><span class="hs-glyph">@</span><span class="hs-special">(</span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span class="annot"><span class="annottext">Addr#
</span><span class="hs-identifier">_</span></span><span> </span><span id="local-6989586621679534568"><span class="annot"><span class="annottext">ForeignPtrContents
</span><a href="#local-6989586621679534568"><span class="hs-identifier hs-var">r</span></a></span></span><span class="hs-special">)</span><span> </span><span id="local-6989586621679534567"><span class="annot"><span class="annottext">Ptr a -&gt; IO b
</span><a href="#local-6989586621679534567"><span class="hs-identifier hs-var">f</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(State# RealWorld -&gt; (# State# RealWorld, b #)) -&gt; IO b
forall a. (State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
</span><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-var">IO</span></a></span><span> </span><span class="annot"><span class="annottext">((State# RealWorld -&gt; (# State# RealWorld, b #)) -&gt; IO b)
-&gt; (State# RealWorld -&gt; (# State# RealWorld, b #)) -&gt; IO b
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679534566"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534566"><span class="hs-identifier hs-var">s</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-531"></span><span>  </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">Ptr a -&gt; IO b
</span><a href="#local-6989586621679534567"><span class="hs-identifier hs-var">f</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">ForeignPtr a -&gt; Ptr a
forall a. ForeignPtr a -&gt; Ptr a
</span><a href="GHC.ForeignPtr.html#unsafeForeignPtrToPtr"><span class="hs-identifier hs-var">unsafeForeignPtrToPtr</span></a></span><span> </span><span class="annot"><span class="annottext">ForeignPtr a
</span><a href="#local-6989586621679534569"><span class="hs-identifier hs-var">fo</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-532"></span><span>    </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span id="local-6989586621679534565"><span class="annot"><span class="annottext">State# RealWorld -&gt; (# State# RealWorld, b #)
</span><a href="#local-6989586621679534565"><span class="hs-identifier hs-var">action#</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">ForeignPtrContents
-&gt; State# RealWorld
-&gt; (State# RealWorld -&gt; (# State# RealWorld, b #))
-&gt; (# State# RealWorld, b #)
</span><a href="../../ghc-prim/src/GHC.Prim.html#keepAlive%23"><span class="hs-identifier hs-var">keepAlive#</span></a></span><span> </span><span class="annot"><span class="annottext">ForeignPtrContents
</span><a href="#local-6989586621679534568"><span class="hs-identifier hs-var">r</span></a></span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534566"><span class="hs-identifier hs-var">s</span></a></span><span> </span><span class="annot"><span class="annottext">State# RealWorld -&gt; (# State# RealWorld, b #)
</span><a href="#local-6989586621679534565"><span class="hs-identifier hs-var">action#</span></a></span><span>
</span><span id="line-533"></span><span>
</span><span id="line-534"></span><span class="hs-comment">-- | This is similar to 'withForeignPtr' but comes with an important caveat:</span><span>
</span><span id="line-535"></span><span class="hs-comment">-- the user must guarantee that the continuation does not diverge (e.g. loop or</span><span>
</span><span id="line-536"></span><span class="hs-comment">-- throw an exception). In exchange for this loss of generality, this function</span><span>
</span><span id="line-537"></span><span class="hs-comment">-- offers the ability of GHC to optimise more aggressively.</span><span>
</span><span id="line-538"></span><span class="hs-comment">--</span><span>
</span><span id="line-539"></span><span class="hs-comment">-- Specifically, applications of the form:</span><span>
</span><span id="line-540"></span><span class="hs-comment">-- @</span><span>
</span><span id="line-541"></span><span class="hs-comment">-- unsafeWithForeignPtr fptr ('Control.Monad.forever' something)</span><span>
</span><span id="line-542"></span><span class="hs-comment">-- @</span><span>
</span><span id="line-543"></span><span class="hs-comment">--</span><span>
</span><span id="line-544"></span><span class="hs-comment">-- See GHC issue #17760 for more information about the unsoundness behavior</span><span>
</span><span id="line-545"></span><span class="hs-comment">-- that this function can result in.</span><span>
</span><span id="line-546"></span><span id="local-6989586621679534563"><span id="local-6989586621679534564"><span class="annot"><a href="GHC.ForeignPtr.html#unsafeWithForeignPtr"><span class="hs-identifier hs-type">unsafeWithForeignPtr</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534564"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Ptr.html#Ptr"><span class="hs-identifier hs-type">Ptr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534564"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534563"><span class="hs-identifier hs-type">b</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534563"><span class="hs-identifier hs-type">b</span></a></span></span></span><span>
</span><span id="line-547"></span><span id="unsafeWithForeignPtr"><span class="annot"><span class="annottext">unsafeWithForeignPtr :: forall a b. ForeignPtr a -&gt; (Ptr a -&gt; IO b) -&gt; IO b
</span><a href="GHC.ForeignPtr.html#unsafeWithForeignPtr"><span class="hs-identifier hs-var hs-var">unsafeWithForeignPtr</span></a></span></span><span> </span><span id="local-6989586621679534559"><span class="annot"><span class="annottext">ForeignPtr a
</span><a href="#local-6989586621679534559"><span class="hs-identifier hs-var">fo</span></a></span></span><span> </span><span id="local-6989586621679534558"><span class="annot"><span class="annottext">Ptr a -&gt; IO b
</span><a href="#local-6989586621679534558"><span class="hs-identifier hs-var">f</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-548"></span><span>  </span><span id="local-6989586621679534557"><span class="annot"><span class="annottext">b
</span><a href="#local-6989586621679534557"><span class="hs-identifier hs-var">r</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">Ptr a -&gt; IO b
</span><a href="#local-6989586621679534558"><span class="hs-identifier hs-var">f</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">ForeignPtr a -&gt; Ptr a
forall a. ForeignPtr a -&gt; Ptr a
</span><a href="GHC.ForeignPtr.html#unsafeForeignPtrToPtr"><span class="hs-identifier hs-var">unsafeForeignPtrToPtr</span></a></span><span> </span><span class="annot"><span class="annottext">ForeignPtr a
</span><a href="#local-6989586621679534559"><span class="hs-identifier hs-var">fo</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-549"></span><span>  </span><span class="annot"><span class="annottext">ForeignPtr a -&gt; IO ()
forall a. ForeignPtr a -&gt; IO ()
</span><a href="GHC.ForeignPtr.html#touchForeignPtr"><span class="hs-identifier hs-var">touchForeignPtr</span></a></span><span> </span><span class="annot"><span class="annottext">ForeignPtr a
</span><a href="#local-6989586621679534559"><span class="hs-identifier hs-var">fo</span></a></span><span>
</span><span id="line-550"></span><span>  </span><span class="annot"><span class="annottext">b -&gt; IO b
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="annot"><span class="annottext">b
</span><a href="#local-6989586621679534557"><span class="hs-identifier hs-var">r</span></a></span><span>
</span><span id="line-551"></span><span>
</span><span id="line-552"></span><span id="local-6989586621679534860"><span class="annot"><a href="GHC.ForeignPtr.html#touchForeignPtr"><span class="hs-identifier hs-type">touchForeignPtr</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534860"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span></span><span>
</span><span id="line-553"></span><span class="hs-comment">-- ^This function ensures that the foreign object in</span><span>
</span><span id="line-554"></span><span class="hs-comment">-- question is alive at the given place in the sequence of IO</span><span>
</span><span id="line-555"></span><span class="hs-comment">-- actions. However, this comes with a significant caveat: the contract above</span><span>
</span><span id="line-556"></span><span class="hs-comment">-- does not hold if GHC can demonstrate that the code preceeding</span><span>
</span><span id="line-557"></span><span class="hs-comment">-- @touchForeignPtr@ diverges (e.g. by looping infinitely or throwing an</span><span>
</span><span id="line-558"></span><span class="hs-comment">-- exception). For this reason, you are strongly advised to use instead</span><span>
</span><span id="line-559"></span><span class="hs-comment">-- 'withForeignPtr' where possible.</span><span>
</span><span id="line-560"></span><span class="hs-comment">--</span><span>
</span><span id="line-561"></span><span class="hs-comment">-- Also, note that this function should not be used to express dependencies</span><span>
</span><span id="line-562"></span><span class="hs-comment">-- between finalizers on 'ForeignPtr's.  For example, if the finalizer for a</span><span>
</span><span id="line-563"></span><span class="hs-comment">-- 'ForeignPtr' @F1@ calls 'touchForeignPtr' on a second 'ForeignPtr' @F2@,</span><span>
</span><span id="line-564"></span><span class="hs-comment">-- then the only guarantee is that the finalizer for @F2@ is never started</span><span>
</span><span id="line-565"></span><span class="hs-comment">-- before the finalizer for @F1@.  They might be started together if for</span><span>
</span><span id="line-566"></span><span class="hs-comment">-- example both @F1@ and @F2@ are otherwise unreachable, and in that case the</span><span>
</span><span id="line-567"></span><span class="hs-comment">-- scheduler might end up running the finalizer for @F2@ first.</span><span>
</span><span id="line-568"></span><span class="hs-comment">--</span><span>
</span><span id="line-569"></span><span class="hs-comment">-- In general, it is not recommended to use finalizers on separate</span><span>
</span><span id="line-570"></span><span class="hs-comment">-- objects with ordering constraints between them.  To express the</span><span>
</span><span id="line-571"></span><span class="hs-comment">-- ordering robustly requires explicit synchronisation using @MVar@s</span><span>
</span><span id="line-572"></span><span class="hs-comment">-- between the finalizers, but even then the runtime sometimes runs</span><span>
</span><span id="line-573"></span><span class="hs-comment">-- multiple finalizers sequentially in a single thread (for</span><span>
</span><span id="line-574"></span><span class="hs-comment">-- performance reasons), so synchronisation between finalizers could</span><span>
</span><span id="line-575"></span><span class="hs-comment">-- result in artificial deadlock.  Another alternative is to use</span><span>
</span><span id="line-576"></span><span class="hs-comment">-- explicit reference counting.</span><span>
</span><span id="line-577"></span><span class="hs-comment">--</span><span>
</span><span id="line-578"></span><span id="touchForeignPtr"><span class="annot"><span class="annottext">touchForeignPtr :: forall a. ForeignPtr a -&gt; IO ()
</span><a href="GHC.ForeignPtr.html#touchForeignPtr"><span class="hs-identifier hs-var hs-var">touchForeignPtr</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span class="annot"><span class="annottext">Addr#
</span><span class="hs-identifier">_</span></span><span> </span><span id="local-6989586621679534556"><span class="annot"><span class="annottext">ForeignPtrContents
</span><a href="#local-6989586621679534556"><span class="hs-identifier hs-var">r</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">ForeignPtrContents -&gt; IO ()
</span><a href="GHC.ForeignPtr.html#touch"><span class="hs-identifier hs-var">touch</span></a></span><span> </span><span class="annot"><span class="annottext">ForeignPtrContents
</span><a href="#local-6989586621679534556"><span class="hs-identifier hs-var">r</span></a></span><span>
</span><span id="line-579"></span><span>
</span><span id="line-580"></span><span class="annot"><a href="GHC.ForeignPtr.html#touch"><span class="hs-identifier hs-type">touch</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtrContents"><span class="hs-identifier hs-type">ForeignPtrContents</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-581"></span><span id="touch"><span class="annot"><span class="annottext">touch :: ForeignPtrContents -&gt; IO ()
</span><a href="GHC.ForeignPtr.html#touch"><span class="hs-identifier hs-var hs-var">touch</span></a></span></span><span> </span><span id="local-6989586621679534555"><span class="annot"><span class="annottext">ForeignPtrContents
</span><a href="#local-6989586621679534555"><span class="hs-identifier hs-var">r</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(State# RealWorld -&gt; (# State# RealWorld, () #)) -&gt; IO ()
forall a. (State# RealWorld -&gt; (# State# RealWorld, a #)) -&gt; IO a
</span><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-var">IO</span></a></span><span> </span><span class="annot"><span class="annottext">((State# RealWorld -&gt; (# State# RealWorld, () #)) -&gt; IO ())
-&gt; (State# RealWorld -&gt; (# State# RealWorld, () #)) -&gt; IO ()
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679534554"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534554"><span class="hs-identifier hs-var">s</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">ForeignPtrContents -&gt; State# RealWorld -&gt; State# RealWorld
</span><a href="../../ghc-prim/src/GHC.Prim.html#touch%23"><span class="hs-identifier hs-var">touch#</span></a></span><span> </span><span class="annot"><span class="annottext">ForeignPtrContents
</span><a href="#local-6989586621679534555"><span class="hs-identifier hs-var">r</span></a></span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534554"><span class="hs-identifier hs-var">s</span></a></span><span> </span><span class="hs-keyword">of</span><span> </span><span id="local-6989586621679534553"><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534553"><span class="hs-identifier hs-var">s'</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(#</span><span> </span><span class="annot"><span class="annottext">State# RealWorld
</span><a href="#local-6989586621679534553"><span class="hs-identifier hs-var">s'</span></a></span><span class="hs-special">,</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span> </span><span class="hs-special">#)</span><span>
</span><span id="line-582"></span><span>
</span><span id="line-583"></span><span id="local-6989586621679534947"><span class="annot"><a href="GHC.ForeignPtr.html#unsafeForeignPtrToPtr"><span class="hs-identifier hs-type">unsafeForeignPtrToPtr</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534947"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.Ptr.html#Ptr"><span class="hs-identifier hs-type">Ptr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534947"><span class="hs-identifier hs-type">a</span></a></span></span><span>
</span><span id="line-584"></span><span class="hs-comment">-- ^This function extracts the pointer component of a foreign</span><span>
</span><span id="line-585"></span><span class="hs-comment">-- pointer.  This is a potentially dangerous operations, as if the</span><span>
</span><span id="line-586"></span><span class="hs-comment">-- argument to 'unsafeForeignPtrToPtr' is the last usage</span><span>
</span><span id="line-587"></span><span class="hs-comment">-- occurrence of the given foreign pointer, then its finalizer(s) will</span><span>
</span><span id="line-588"></span><span class="hs-comment">-- be run, which potentially invalidates the plain pointer just</span><span>
</span><span id="line-589"></span><span class="hs-comment">-- obtained.  Hence, 'touchForeignPtr' must be used</span><span>
</span><span id="line-590"></span><span class="hs-comment">-- wherever it has to be guaranteed that the pointer lives on - i.e.,</span><span>
</span><span id="line-591"></span><span class="hs-comment">-- has another usage occurrence.</span><span>
</span><span id="line-592"></span><span class="hs-comment">--</span><span>
</span><span id="line-593"></span><span class="hs-comment">-- To avoid subtle coding errors, hand written marshalling code</span><span>
</span><span id="line-594"></span><span class="hs-comment">-- should preferably use 'Foreign.ForeignPtr.withForeignPtr' rather</span><span>
</span><span id="line-595"></span><span class="hs-comment">-- than combinations of 'unsafeForeignPtrToPtr' and</span><span>
</span><span id="line-596"></span><span class="hs-comment">-- 'touchForeignPtr'.  However, the latter routines</span><span>
</span><span id="line-597"></span><span class="hs-comment">-- are occasionally preferred in tool generated marshalling code.</span><span>
</span><span id="line-598"></span><span id="unsafeForeignPtrToPtr"><span class="annot"><span class="annottext">unsafeForeignPtrToPtr :: forall a. ForeignPtr a -&gt; Ptr a
</span><a href="GHC.ForeignPtr.html#unsafeForeignPtrToPtr"><span class="hs-identifier hs-var hs-var">unsafeForeignPtrToPtr</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span id="local-6989586621679534552"><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534552"><span class="hs-identifier hs-var">fo</span></a></span></span><span> </span><span class="annot"><span class="annottext">ForeignPtrContents
</span><span class="hs-identifier">_</span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Addr# -&gt; Ptr a
forall a. Addr# -&gt; Ptr a
</span><a href="GHC.Ptr.html#Ptr"><span class="hs-identifier hs-var">Ptr</span></a></span><span> </span><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534552"><span class="hs-identifier hs-var">fo</span></a></span><span>
</span><span id="line-599"></span><span>
</span><span id="line-600"></span><span id="local-6989586621679534855"><span id="local-6989586621679534856"><span class="annot"><a href="GHC.ForeignPtr.html#castForeignPtr"><span class="hs-identifier hs-type">castForeignPtr</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534856"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534855"><span class="hs-identifier hs-type">b</span></a></span></span></span><span>
</span><span id="line-601"></span><span class="hs-comment">-- ^This function casts a 'ForeignPtr'</span><span>
</span><span id="line-602"></span><span class="hs-comment">-- parameterised by one type into another type.</span><span>
</span><span id="line-603"></span><span id="castForeignPtr"><span class="annot"><span class="annottext">castForeignPtr :: forall a b. ForeignPtr a -&gt; ForeignPtr b
</span><a href="GHC.ForeignPtr.html#castForeignPtr"><span class="hs-identifier hs-var hs-var">castForeignPtr</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">ForeignPtr a -&gt; ForeignPtr b
</span><a href="../../ghc-prim/src/GHC.Prim.html#coerce"><span class="hs-identifier hs-var">coerce</span></a></span><span>
</span><span id="line-604"></span><span>
</span><span id="line-605"></span><span id="local-6989586621679534850"><span id="local-6989586621679534851"><span class="annot"><a href="GHC.ForeignPtr.html#plusForeignPtr"><span class="hs-identifier hs-type">plusForeignPtr</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534851"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Int"><span class="hs-identifier hs-type">Int</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534850"><span class="hs-identifier hs-type">b</span></a></span></span></span><span>
</span><span id="line-606"></span><span class="hs-comment">-- ^Advances the given address by the given offset in bytes.</span><span>
</span><span id="line-607"></span><span class="hs-comment">--</span><span>
</span><span id="line-608"></span><span class="hs-comment">-- The new 'ForeignPtr' shares the finalizer of the original,</span><span>
</span><span id="line-609"></span><span class="hs-comment">-- equivalent from a finalization standpoint to just creating another</span><span>
</span><span id="line-610"></span><span class="hs-comment">-- reference to the original. That is, the finalizer will not be</span><span>
</span><span id="line-611"></span><span class="hs-comment">-- called before the new 'ForeignPtr' is unreachable, nor will it be</span><span>
</span><span id="line-612"></span><span class="hs-comment">-- called an additional time due to this call, and the finalizer will</span><span>
</span><span id="line-613"></span><span class="hs-comment">-- be called with the same address that it would have had this call</span><span>
</span><span id="line-614"></span><span class="hs-comment">-- not happened, *not* the new address.</span><span>
</span><span id="line-615"></span><span class="hs-comment">--</span><span>
</span><span id="line-616"></span><span class="hs-comment">-- @since 4.10.0.0</span><span>
</span><span id="line-617"></span><span id="plusForeignPtr"><span class="annot"><span class="annottext">plusForeignPtr :: forall a b. ForeignPtr a -&gt; Int -&gt; ForeignPtr b
</span><a href="GHC.ForeignPtr.html#plusForeignPtr"><span class="hs-identifier hs-var hs-var">plusForeignPtr</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span id="local-6989586621679534550"><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534550"><span class="hs-identifier hs-var">addr</span></a></span></span><span> </span><span id="local-6989586621679534549"><span class="annot"><span class="annottext">ForeignPtrContents
</span><a href="#local-6989586621679534549"><span class="hs-identifier hs-var">c</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-special">(</span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#I%23"><span class="hs-identifier hs-type">I#</span></a></span><span> </span><span id="local-6989586621679534548"><span class="annot"><span class="annottext">Int#
</span><a href="#local-6989586621679534548"><span class="hs-identifier hs-var">d</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Addr# -&gt; ForeignPtrContents -&gt; ForeignPtr b
forall a. Addr# -&gt; ForeignPtrContents -&gt; ForeignPtr a
</span><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-var">ForeignPtr</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Addr# -&gt; Int# -&gt; Addr#
</span><a href="../../ghc-prim/src/GHC.Prim.html#plusAddr%23"><span class="hs-identifier hs-var">plusAddr#</span></a></span><span> </span><span class="annot"><span class="annottext">Addr#
</span><a href="#local-6989586621679534550"><span class="hs-identifier hs-var">addr</span></a></span><span> </span><span class="annot"><span class="annottext">Int#
</span><a href="#local-6989586621679534548"><span class="hs-identifier hs-var">d</span></a></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">ForeignPtrContents
</span><a href="#local-6989586621679534549"><span class="hs-identifier hs-var">c</span></a></span><span>
</span><span id="line-618"></span><span>
</span><span id="line-619"></span><span class="hs-comment">-- | Causes the finalizers associated with a foreign pointer to be run</span><span>
</span><span id="line-620"></span><span class="hs-comment">-- immediately. The foreign pointer must not be used again after this</span><span>
</span><span id="line-621"></span><span class="hs-comment">-- function is called. If the foreign pointer does not support finalizers,</span><span>
</span><span id="line-622"></span><span class="hs-comment">-- this is a no-op.</span><span>
</span><span id="line-623"></span><span id="local-6989586621679534547"><span class="annot"><a href="GHC.ForeignPtr.html#finalizeForeignPtr"><span class="hs-identifier hs-type">finalizeForeignPtr</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679534547"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span></span><span>
</span><span id="line-624"></span><span id="finalizeForeignPtr"><span class="annot"><span class="annottext">finalizeForeignPtr :: forall a. ForeignPtr a -&gt; IO ()
</span><a href="GHC.ForeignPtr.html#finalizeForeignPtr"><span class="hs-identifier hs-var hs-var">finalizeForeignPtr</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.ForeignPtr.html#ForeignPtr"><span class="hs-identifier hs-type">ForeignPtr</span></a></span><span> </span><span class="annot"><span class="annottext">Addr#
</span><span class="hs-identifier">_</span></span><span> </span><span id="local-6989586621679534544"><span class="annot"><span class="annottext">ForeignPtrContents
</span><a href="#local-6989586621679534544"><span class="hs-identifier hs-var">c</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">ForeignPtrContents
</span><a href="#local-6989586621679534544"><span class="hs-identifier hs-var">c</span></a></span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-625"></span><span>  </span><span class="annot"><a href="GHC.ForeignPtr.html#PlainForeignPtr"><span class="hs-identifier hs-type">PlainForeignPtr</span></a></span><span> </span><span id="local-6989586621679534543"><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534543"><span class="hs-identifier hs-var">ref</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">IORef Finalizers -&gt; IO ()
</span><a href="GHC.ForeignPtr.html#foreignPtrFinalizer"><span class="hs-identifier hs-var">foreignPtrFinalizer</span></a></span><span> </span><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534543"><span class="hs-identifier hs-var">ref</span></a></span><span>
</span><span id="line-626"></span><span>  </span><span class="annot"><a href="GHC.ForeignPtr.html#MallocPtr"><span class="hs-identifier hs-type">MallocPtr</span></a></span><span> </span><span class="annot"><span class="annottext">MutableByteArray# RealWorld
</span><span class="hs-identifier">_</span></span><span> </span><span id="local-6989586621679534542"><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534542"><span class="hs-identifier hs-var">ref</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">IORef Finalizers -&gt; IO ()
</span><a href="GHC.ForeignPtr.html#foreignPtrFinalizer"><span class="hs-identifier hs-var">foreignPtrFinalizer</span></a></span><span> </span><span class="annot"><span class="annottext">IORef Finalizers
</span><a href="#local-6989586621679534542"><span class="hs-identifier hs-var">ref</span></a></span><span>
</span><span id="line-627"></span><span>  </span><span class="annot"><a href="GHC.ForeignPtr.html#PlainPtr"><span class="hs-identifier hs-type">PlainPtr</span></a></span><span class="hs-special">{</span><span class="hs-special">}</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">() -&gt; IO ()
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-628"></span><span>  </span><span class="annot"><a href="GHC.ForeignPtr.html#FinalPtr"><span class="hs-identifier hs-type">FinalPtr</span></a></span><span class="hs-special">{</span><span class="hs-special">}</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">() -&gt; IO ()
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-629"></span><span>
</span><span id="line-630"></span><span class="hs-comment">{- $commentary

This is a high-level overview of how 'ForeignPtr' works.
The implementation of 'ForeignPtr' must accomplish several goals:

1. Invoke a finalizer once a foreign pointer becomes unreachable.
2. Support augmentation of finalizers, i.e. 'addForeignPtrFinalizer'.
   As a motivating example, suppose that the payload of a foreign
   pointer is C struct @bar@ that has an optionally NULL pointer field
   @foo@ to an unmanaged heap object. Initially, @foo@ is NULL, and
   later the program uses @malloc@, initializes the object, and assigns
   @foo@ the address returned by @malloc@. When the foreign pointer
   becomes unreachable, it is now necessary to first @free@ the object
   pointed to by @foo@ and then invoke whatever finalizer was associated
   with @bar@. That is, finalizers must be invoked in the opposite order
   they are added.
3. Allow users to invoke a finalizer promptly if they know that the
   foreign pointer is unreachable, i.e. 'finalizeForeignPtr'.

How can these goals be accomplished? Goal 1 suggests that weak references
and finalizers (via 'Weak#' and 'mkWeak#') are necessary. But how should
they be used and what should their key be?  Certainly not 'ForeignPtr' or
'ForeignPtrContents'. See the warning in &quot;GHC.Weak&quot; about weak pointers with
lifted (non-primitive) keys. The two finalizer-supporting data constructors of
'ForeignPtr' have an @'IORef' 'Finalizers'@ (backed by 'MutVar#') field.
This gets used in two different ways depending on the kind of finalizer:

* 'HaskellFinalizers': The first @addForeignPtrConcFinalizer_@ call uses
  'mkWeak#' to attach the finalizer @foreignPtrFinalizer@ to the 'MutVar#'.
  The resulting 'Weak#' is discarded (see @addForeignPtrConcFinalizer_@).
  Subsequent calls to @addForeignPtrConcFinalizer_@ (goal 2) just add
  finalizers onto the list in the 'HaskellFinalizers' data constructor.
* 'CFinalizers': The first 'addForeignPtrFinalizer' call uses
  'mkWeakNoFinalizer#' to create a 'Weak#'. The 'Weak#' is preserved in the
  'CFinalizers' data constructor. Both the first call and subsequent
  calls (goal 2) use 'addCFinalizerToWeak#' to attach finalizers to the
  'Weak#' itself. Also, see Note [MallocPtr finalizers] for discussion of
  the key and value of this 'Weak#'.

In either case, the runtime invokes the appropriate finalizers when the
'ForeignPtr' becomes unreachable.
-}</span><span>
</span><span id="line-672"></span></pre></body></html>